1

从相机拍照后,我尝试从 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)

请帮我修复这个错误

4

2 回答 2

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);
            }
        }
    }
于 2013-03-04T10:08:56.157 回答
0

这是解决方案 ->只需将此文件复制粘贴到您的代码中并使用以下代码获取文件路径:-

   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);
        }

--> 如果你愿意,可以省略方法:)

于 2015-11-24T07:16:34.613 回答