3

我有一个处理从图库中选择的图片的应用程序。但是这样的堆栈跟踪在 ICS 上发生了崩溃:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, Intent { dat=content://com.google.android.gallery3d.provider/picasa/item/5629933319233370946 }} to activity {com.myexample/com.myexample.activities.UserProfileActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
at android.app.ActivityThread.access$1100(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException
at java.io.File.fixSlashes(File.java:185)
at java.io.File.(File.java:134)
at com.jettaxi.activities.UserProfileActivity.onActivityResult(SourceFile:316)
at android.app.Activity.dispatchActivityResult(Activity.java:4649)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
... 11 more

我找到了一些有关此类错误的信息,但是我可以避免这种情况以及如何避免吗?

编辑:我的代码:

private void getImageFromGallery() {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(galleryIntent, GALLERY_PIC_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            try {
                captureFile = new File(getCaptureFilePath());
                captureUri = Uri.fromFile(captureFile);

                Bitmap scaledBitmap = decodeFileAndResize(captureFile);
                saveResizedAndCompressedBitmap(scaledBitmap);

                Bitmap rotatedBitmap = convertToRotatedBitmap(scaledBitmap);
                driverPhoto.setImageBitmap(rotatedBitmap);

                if (rotatedBitmap != scaledBitmap) {
                    scaledBitmap.recycle();
                    scaledBitmap = null;
                    System.gc();
                }
            } catch (IOException e) {
                BugSenseHandler.log(TAG, e);
            }
        } else if (requestCode == GALLERY_PIC_REQUEST) {
            try {
                captureFile = new File(getCaptureFilePath());
                captureUri = Uri.fromFile(captureFile);

                Uri chosenUri = data.getData();

                String[] filePathColumn = {MediaStore.Images.Media.DATA};

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

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

                File chosenFile = new File(filePath);

                Bitmap selectedImageBitmap = decodeFileAndResize(chosenFile);
                saveResizedAndCompressedBitmap(selectedImageBitmap);
                driverPhoto.setImageBitmap(selectedImageBitmap);
            } catch (IOException ie) {
                BugSenseHandler.log(TAG, ie);
            }
        }
    }
}
4

2 回答 2

10

这可能有助于防止它列出不在 SD 卡上的项目:

galleryIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
于 2012-06-23T00:01:40.590 回答
0

堆栈跟踪表明该图像来自 picasa 相册。所以你的sd卡上没有文件。在这种情况下用于getContentResolver().openInputStream(intent.getData())加载图像。

于 2012-05-11T09:36:41.520 回答