如何正确地从URI获取真正的文件路径?

时间:2012-08-03 11:05:20

标签: android cursor uri android-camera

我从相机拍照后尝试从URI获取真实的文件路径。我这样做:

private String getRealPathFromURI(Uri contentURI) {
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
}

和此:

 public String getRealPathFromURI(Uri contentUri) {
     String[] proj = { MediaColumns.DATA };
     Cursor cursor = managedQuery(contentUri, proj, null, null, null);
     int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
     cursor.moveToFirst();
     return cursor.getString(column_index);
     }

private String getRealPathFromURI(Uri selectedVideoUri) {
    String filePath;
    final String[] filePathColumn = new String[] { MediaColumns.DATA };

    Cursor cursor = getContentResolver().query(selectedVideoUri,
            filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    filePath = cursor.getString(columnIndex);
    cursor.close();
    return filePath;
}

在大多数手机上,一切正常。但在某些手机应用程序崩溃时出现错误:

08-03 14:55:13.092: E/AndroidRuntime(1676):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(SourceFile:190)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at android.os.Handler.dispatchMessage(SourceFile:130)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at android.os.Looper.loop(SourceFile:351)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at android.app.ActivityThread.main(SourceFile:4070)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at java.lang.reflect.Method.invokeNative(Native Method)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at java.lang.reflect.Method.invoke(Method.java:538)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(SourceFile:901)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at com.android.internal.os.ZygoteInit.main(SourceFile:659)
08-03 14:55:13.092: E/AndroidRuntime(1676):     at dalvik.system.NativeStart.main(Native Method)
08-03 14:55:19.823: I/System.out(1728): --MyLocationListener is created!
08-03 14:55:19.838: D/AndroidRuntime(1728): Shutting down VM
08-03 14:55:19.838: W/dalvikvm(1728): threadid=1: thread exiting with uncaught exception (group=0x4001c648)
08-03 14:55:19.842: E/AndroidRuntime(1728): FATAL EXCEPTION: main
08-03 14:55:19.842: E/AndroidRuntime(1728): java.lang.RuntimeException: Unable to create service com.webparadox.tonquer.MyLocationListener: java.lang.NullPointerException
08-03 14:55:19.842: E/AndroidRuntime(1728):     at android.app.ActivityThread.handleCreateService(SourceFile:2221)
08-03 14:55:19.842: E/AndroidRuntime(1728):     at android.app.ActivityThread.access$2500(SourceFile:160)

请帮我修复此错误

2 个答案:

答案 0 :(得分:0)

希望以下代码能够提供帮助

   String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA,
    };
    Cursor mImageCursor =   getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
            null, null);

    if (mImageCursor != null) 
    {
        if (mImageCursor.getCount() == 0) 
        {
            setContentView(R.layout.no_image_found);
                        } 
        else
        {

            while (mImageCursor.moveToNext()){
                Images im = new Images();
                int column_index = mImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                im.setFilePath(mImageCursor.getString(column_index));
                image_column_index = mImageCursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media._ID);
                int id = mImageCursor.getInt(image_column_index);
                im.setUri(Uri.withAppendedPath(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, ""
                                + id));
                arrayOfImages.add(im);
            }
        }
    }

答案 1 :(得分:0)

Here is the solution - >只需将此文件复制粘贴到您的代码中,然后使用下面的代码获取文件路径: -

   filepath= FileUtils.getPath(getActivity(),selectedImageUri);

注意: -

 else if (isDownloadsDocument(uri)) {
            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }

- >如果你想要,可以省略方法:)