[안드로이드] sdcard 이미지 불러올때
by 안드초보1 | 16.03.22 16:30 | 4,469 hit

이미지 절대 경로 가져올때 밑에처럼 에러가 나오는데... 왜 그런건지 몰르겠습니다

//선택한 이미지의 uri를 읽어온다.
Uri selPhotoUri = intent.getData();

Log.d("aaa2", "aaa2");
Log.d("aaa2", String.valueOf(selPhotoUri));

//업로드할 서버의 url 주소
String urlString = "http://cs.seumtech.kr/aa.png";
//절대경로를 획득한다!!! 중요~
Cursor c = getContentResolver().query(Uri.parse(selPhotoUri.toString()), null,null,null,null);
Log.d("aaa3", "aaa3");
Log.d("aaa3", String.valueOf(c));

c.moveToNext();
//업로드할 파일의 절대경로 얻어오기("_data") 로 해도 된다.
String absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
Log.d("aaa4", "aaa4");
Log.d("aaa4", absolutePath);

Log.e("###파일의 절대 경로###", absolutePath);
//파일 업로드 시작!
HttpFileUpload(urlString ,"", absolutePath);



log보면 3까지는 잘찍히고 96번째줄이

String absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));


에러메시지

03-22 16:27:54.491 5519-5519/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.aa.testim5, PID: 5519
                                                 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:1078 flg=0x1 }} to activity {com.example.aa.testim5/com.example.aa.testim5.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                     at android.app.ActivityThread.deliverResults(ActivityThread.java:4920)
                                                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4963)
                                                     at android.app.ActivityThread.access$1600(ActivityThread.java:221)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:158)
                                                     at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                  Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                     at android.database.CursorWindow.nativeGetString(Native Method)
                                                     at android.database.CursorWindow.getString(CursorWindow.java:451)
                                                     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
                                                     at android.database.CursorWrapper.getString(CursorWrapper.java:137)
                                                     at com.example.aa.testim5.MainActivity.onActivityResult(MainActivity.java:96)
                                                     at android.app.Activity.dispatchActivityResult(Activity.java:7137)
                                                     at android.app.ActivityThread.deliverResults(ActivityThread.java:4916)
                                                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4963) 
                                                     at android.app.ActivityThread.access$1600(ActivityThread.java:221) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:158) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 



질문자가 자신의 포인트 5 점을 걸었습니다.
답변하시면 포인트 2을, 답변이 채택되면 포인트 4을 드립니다.
질문답변 게시판에서 궁금한 사항을 해결하셨다면, 애써 답변해주신 분께 잘 되었다고 따뜻한 댓글 한마디 남겨주세요.
그리고 답변해주신 분의 글을 '추천' 해주세요.
추천받으신 분에게 1포인트가, 추천하신 분에게도 1포인트가 적립됩니다. ^^

by 안사드 2016.03.22 16:33
해당 컬럼 없대요.. 그래서 -1리턴했고 그것을 가져오려고 하니 에러난거라네요 에러메시지가
추천 1 반대 0
by nicehee 2016.03.22 17:33

public String getPathFromUri(Uri uri){

Cursor cursor = getContentResolver().query(uri, null, null, null, null );

cursor.moveToNext(); 

String path = cursor.getString( cursor.getColumnIndex( "_data" ) );

c.close();


return path;

}

http://ccdev.tistory.com/23

추천 2 반대 0
by 안드초보1 2016.03.22 19:43

음...인덱스0으로 나옵니다..

113번째줄이 int id = c.getInt( c.getColumnIndex( "_id" ) );    입니다

파일은

String fileName= file:///sdcard/notes/aa.png;

로 했는데.. 제 핸드폰으로 sdcard/notes들어가면 aa.png 이미지가 있습니다

에러메시지

03-22 19:40:18.638 21801-21801/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.aa.testim5, PID: 21801
                                                   java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:1078 flg=0x1 }} to activity {com.example.aa.testim5/com.example.aa.testim5.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
                                                       at android.app.ActivityThread.deliverResults(ActivityThread.java:4920)
                                                       at android.app.ActivityThread.handleSendResult(ActivityThread.java:4963)
                                                       at android.app.ActivityThread.access$1600(ActivityThread.java:221)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:158)
                                                       at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                    Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
                                                       at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
                                                       at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                                                       at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
                                                       at android.database.CursorWrapper.getInt(CursorWrapper.java:122)
                                                       at com.example.aa.testim5.MainActivity.onActivityResult(MainActivity.java:113)
                                                       at android.app.Activity.dispatchActivityResult(Activity.java:7137)
                                                       at android.app.ActivityThread.deliverResults(ActivityThread.java:4916)
                                                       at android.app.ActivityThread.handleSendResult(ActivityThread.java:4963) 
                                                       at android.app.ActivityThread.access$1600(ActivityThread.java:221) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:158) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

[@nicehee]

추천 0 반대 0
by nicehee 2016.03.23 10:07
[@안드초보1]
cursor.moveToFirst() 로 해보셔요
추천 3 반대 0
by 안드초보1 2016.03.23 18:57
c.moveToFirst();
c.moveToNext();
했더니 밑에처럼 에러 메시지가 나옵니다. 위에처럼 똑같이 에러메시지인데..음..

에러메시지

03-23 18:56:12.751 7582-7582/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.aa.testim5, PID: 7582
                                                 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:1078 flg=0x1 }} to activity {com.example.aa.testim5/com.example.aa.testim5.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
                                                     at android.app.ActivityThread.deliverResults(ActivityThread.java:4920)
                                                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4963)
                                                     at android.app.ActivityThread.access$1600(ActivityThread.java:221)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:158)
                                                     at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                  Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
                                                     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
                                                     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                                                     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
                                                     at android.database.CursorWrapper.getInt(CursorWrapper.java:122)
                                                     at com.example.aa.testim5.MainActivity.onActivityResult(MainActivity.java:121)
                                                     at android.app.Activity.dispatchActivityResult(Activity.java:7137)
                                                     at android.app.ActivityThread.deliverResults(ActivityThread.java:4916)
                                                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4963) 
                                                     at android.app.ActivityThread.access$1600(ActivityThread.java:221) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:158) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

[@nicehee]

추천 0 반대 0
by 한승연 2016.03.23 14:13
커서는 ... 위치를 처음에 잡아주고 시작하셔야 하는데 그부분이 빠진거 같네요 

히님 추천드립니당.
추천 1 반대 0
by 한승연 2016.03.24 13:24
if( c.moveToFirst() )  // return boolean
{
do{
// c.get("~~");
}while(c.moveToNext());
}else{
// file not found
c.Close();
//아마 여기에 올거 같은데.. 파일 
	//Cursor c = getContentResolver().query(Uri.parse(selPhotoUri.toString()), null,null,null,null);
	// 커서 데이터가 없다는 거 같으니 .. uri 값 다시 확인 해보시는게 ..
	
}
추천 1 반대 0
안드로이드사이드 PC버전 로그인