获取java.lang.RuntimeException:从NPE传递结果ResultInfo失败,这显然是由于我的getRealPathFromURI函数中的调用导致的。

视频捕获效果很好,但是图像捕获会抛出NPE。图像和视频都可以在我的Evo上正常工作。

03-30 09:34:25.725 D/ZoorniApp( 2509): Handling activity result. requestCode:12345 resultCode:-1
03-30 09:34:25.733 D/AndroidRuntime( 2509): Shutting down VM
03-30 09:34:25.733 W/dalvikvm( 2509): threadid=3: thread exiting with uncaught exception (group=0x4001e2e0)
03-30 09:34:25.733 E/AndroidRuntime( 2509): Uncaught handler: thread main exiting due to uncaught exception
03-30 09:34:25.741 E/AndroidRuntime( 2509): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=12345, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.mobile.zoorni/com.mobile.zoorni.ZoorniMobile}: java.lang.NullPointerException
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.app.ActivityThread.deliverResults(ActivityThread.java:3391)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3433)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.app.ActivityThread.access$2900(ActivityThread.java:121)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.os.Looper.loop(Looper.java:136)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.app.ActivityThread.main(ActivityThread.java:4425)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at java.lang.reflect.Method.invoke(Method.java:521)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at dalvik.system.NativeStart.main(Native Method)
03-30 09:34:25.741 E/AndroidRuntime( 2509): Caused by: java.lang.NullPointerException
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.content.ContentResolver.acquireProvider(ContentResolver.java:757)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.content.ContentResolver.query(ContentResolver.java:200)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.app.Activity.managedQuery(Activity.java:1495)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at com.mobile.zoorni.ZoorniMobile.getRealPathFromURI(ZoorniMobile.java:287)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at com.mobile.zoorni.ZoorniMobile.onActivityResult(ZoorniMobile.java:251)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.app.Activity.dispatchActivityResult(Activity.java:3828)
03-30 09:34:25.741 E/AndroidRuntime( 2509): at android.app.ActivityThread.deliverResults(ActivityThread.java:3387)


相关功能如下:

public String getRealPathFromURI(Uri contentUri) {
    String column;

    column = "";
    if (fileType == "picture") {
        column = MediaStore.Images.Media.DATA;
    }
    if (fileType == "video") {
        column = MediaStore.Video.Media.DATA;
    }

    String[] proj = { column };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null); // here lies the exception!
    int column_index = cursor.getColumnIndex( column );
    if (column_index == -1) {
        alert("Path missing", "Could not locate the file requested", this);
        return "";
    }
    cursor.moveToFirst();
    return cursor.getString(column_index);
}


这是经过清理的相关代码(只是删除了客户信息)

/*
 * Call the camera activity for video or picture
 */
protected void startCaptureIntent(String actionCode, int requestCode, int media) {
    Intent i = new Intent(actionCode);

    if (media == MEDIA_VIDEO) {
        i.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    }

    startActivityForResult(i, requestCode);
}

/*
 * Handle the activity result
 *
 * @see android.app.Activity#onActivityResult(int, int,
 * android.content.Intent)
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    //super.onActivityResult(requestCode, resultCode, intent);

    Log.d("MYDEBUGGING", "Handling activity result. requestCode:"   + requestCode + " resultCode:" + resultCode);

    if (resultCode == Activity.RESULT_CANCELED) {
        pic_upload_button.setEnabled(true);
        video_upload_button.setEnabled(true);
        fileType = "none";
        showToast(this, "Request canceled, Touch the picture or image button to try again");
        return;
    }

    switch (requestCode) {
    case CAMERA_PIC_REQUEST:
        switch (resultCode) {
        case Activity.RESULT_OK:
            postType = requestCode;
            fileType = "picture";

            // Seems that this is the only way to be sure I end up with an actual file.
            filePath = getRealPathFromURI(intent.getData());

            if (filePath != null) {
                showToast(this, "Image ready to be shared");
            } else {
                showToast(this, "Something went wrong. Image could not be captured.");
            }
            break;
        default:
            alert("Activity failed", "Could not create picture file", this);
        }
        break;
    case CAMERA_VID_REQUEST:
        switch (resultCode) {
            case Activity.RESULT_OK:
            postType = requestCode;
            fileType = "video";

            // Seems that this is the only way to be sure I end up with a video file.
            filePath = getRealPathFromURI(intent.getData());

            if (filePath != null) {
                showToast(this, "Video ready to be shared");
            } else {
                showToast(this, "Something went wrong. Video could not be captured.");
            }
            break;
        default:
            alert("Activity failed", "Could not create video file", this);
        }
        break;
    }
}


fileType和postType是该类的全局变量,用于向http上传器指示要发送的文件类型。根据用户选择,actionCode可以是MediaStore.ACTION_VIDEO_CAPTURE或MediaStore.ACTION_IMAGE_CAPTURE。

有什么想法吗?

最佳答案

我的猜测是contentUri或filetype值不正确,因此managedQuery调用失败。

10-01 05:20