【发布时间】:2015-10-12 02:00:42
【问题描述】:
我正在尝试获取图库或相机中图像的绝对路径。
相机意图打开后,我单击图像并尝试从 URI 获取文件路径。请看代码位。
//In the OnActivityResult:
Uri uri = data.getData();
File myFile = new File(uri.getPath());
File imageFilePath = new File(getRealPathFromURI(uri));
//The method to fetch path:
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
Log.d("", "cursor = " +cursor);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
int idx = cursor.getColumnIndex(MediaStore.Images.Media.DATA ); // Is this the wrong way to get the image cursor?
Log.d("", "came here = " +idx); // Here idx us -1!!!
cursor.moveToFirst();
result = cursor.getString(idx);
cursor.close();
}
return result;
}
// image pick intent
public void imageFromGallery() {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
添加 logCat:
721-4721/com.coolhydro.copy E/CursorWindow﹕ Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 6 columns.
10-11 18:53:55.495 4721-4721/com.coolhydro.copy D/AndroidRuntime﹕ Shutting down VM
10-11 18:53:55.495 4721-4721/com.coolhydro.copy E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.coolhydro.copy, PID: 4721
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:49 flg=0x1 }} to activity {com.coolhydro.copy/com.coolhydro.copy.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:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51 )
at android.database.CursorWrapper.getString(CursorWrapper.java:137)
at com.coolhydro.copy.MainActivity.getRealPathFromURI(MainActivity.java:80)
at com.coolhydro.copy.MainActivity.onActivityResult(MainActivity.java:52)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-11 18:54:01.237 4721-4730/com.coolhydro.copy W/CursorWrapperInner﹕ Cursor finalized without prior close()
10-11 18:58:55.523 4721-4721/? I/Process﹕ Sending signal. PID: 4721 SIG: 9
谢谢!
【问题讨论】:
-
LogCat 显示的错误是什么?
标签: java android google-apps