cursor.getString返回带有效uri的null

时间:2014-11-27 13:34:16

标签: java android bitmap

突然间程序停止了工作。

我有一个URI:" content://com.android.providers.media.documents/document/image%3A13",图像的文件路径。

选择URI的路径如下:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode,  data);
        if(resultCode == RESULT_OK) {
            try {

                // This bit here
                Bitmap bitmap = getPath(data.getData());
                Log.i("Bitmap", "Bmp: " + data.getData());

            }catch (Exception e){
                Log.e("Error", "Error with setting the image.");
                e.printStackTrace();
            }
        }
    }

因此,调用getPath(),将数据作为URI(URI正确,日志显示)

getPath()

private Bitmap getPath(Uri uri) {

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

        cursor.moveToFirst();

        // it is this line here, it returns null for some reason.
        String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

        // Convert file path into bitmap image using below line.

        Log.i("File Path", "File name: " + filePath); // this comes out as NULL in the logcat

        Bitmap bitmap = BitmapFactory.decodeFile(filePath);

        filePathForUpload = filePath;

        try {
            ExifInterface exif = new ExifInterface(filePath);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
            bitmap = rotateBitmap(bitmap, orientation);
        }catch (Exception e){
            Log.d("Error", "error with bitmap!");
            e.printStackTrace();
        }

        return bitmap;
    }

Logcat输出:

 java.lang.IllegalArgumentException: filename cannot be null
     at android.media.ExifInterface.<init>(ExifInterface.java:121)
     at build.com.build.SubmitPicActivity.getPath(SubmitPicActivity.java:123)
     at build.com.build.SubmitPicActivity.onActivityResult(SubmitPicActivity.java:93)
     at android.app.Activity.dispatchActivityResult(Activity.java:5423)
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
     at android.app.ActivityThread.access$1300(ActivityThread.java:135)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:5017)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:515)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
     at dalvik.system.NativeStart.main(Native Method)

第123行是:String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

第93行是:Bitmap bitmap = getPath(data.getData());

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

A Uri is not necessarily a File。您尝试为File获取Uri的代码从来都不可靠,并且无法继续使用。

请使用Uri以及ContentResolveropenInputStream()等方法正确使用getType()。实质上,您对Uri的处理方式与对Web服务器的URL的处理方式相同,原因大致相同:ContentProvider表示的内容不需要来自您的应用可以访问的普通File