Android onActivityResult()在图像捕获时返回NullPointerException

时间:2013-05-01 10:01:17

标签: java android image nullpointerexception

我在从我开发的相机意图中接收数据捕获时遇到了问题。我使用了android API指南中的代码。谁能告诉我哪里出错?我可以看到LogCat打印输出中存在的who=nulldata=null参数可能导致错误,但我不确定为什么会这样。

相机活动

//ESSENTIAL VARIABLES - DD - 29/04/2013
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int MEDIA_TYPE_IMAGE = 1;
private Uri fileUri;
MenuItem item;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_snap_camera);

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

/**
 * Gets the OutputMediaFileUri and accepts media type as a parameter
 * @param type
 * @return
 */
private static Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/**
 * Method taking media type as a parameter and will save images taken to a public directory on users' device.
 * @param type
 * @return
 */
private static File getOutputMediaFile(int type){

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CrowdSnapCymru");

    if(!mediaStorageDir.exists()){
        if(! mediaStorageDir.mkdirs()){
            Log.d("CrowdSnapCymru", "failed to create photo directory");
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if(type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    }
    else{
        return null;
    }

    return mediaFile;

}

/**
 * Receives the result of Camera intent. 
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if(resultCode == RESULT_OK){
            Toast.makeText(this, "Image saved to: \n" + data.getData() , Toast.LENGTH_LONG).show();
        }
        else if(resultCode == RESULT_CANCELED){
            Toast.makeText(this, "User canceled the image capture", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(this, "Image capture failed. Please try again", Toast.LENGTH_LONG).show();
        }
    }
}

代码加载了Camera应用程序,如果我取消了该应用程序,但是如果我接受了该图像,或者RESULT_OKAY生成了NullPointerException,则会产生成功的结果。

LogCat打印输出

05-01 09:57:48.894: E/AndroidRuntime(974): FATAL EXCEPTION: main
05-01 09:57:48.894: E/AndroidRuntime(974): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {rcahmw.prototype.crowdsnapcymru/rcahmw.prototype.crowdsnapcymru.SnapCamera}: java.lang.NullPointerException
05-01 09:57:48.894: E/AndroidRuntime(974):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
05-01 09:57:48.894: E/AndroidRuntime(974):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
05-01 09:57:48.894: E/AndroidRuntime(974):  at android.app.ActivityThread.access$1100(ActivityThread.java:141)
05-01 09:57:48.894: E/AndroidRuntime(974):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
05-01 09:57:48.894: E/AndroidRuntime(974):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 09:57:48.894: E/AndroidRuntime(974):  at android.os.Looper.loop(Looper.java:137)
05-01 09:57:48.894: E/AndroidRuntime(974):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-01 09:57:48.894: E/AndroidRuntime(974):  at java.lang.reflect.Method.invokeNative(Native Method)
05-01 09:57:48.894: E/AndroidRuntime(974):  at java.lang.reflect.Method.invoke(Method.java:511)
05-01 09:57:48.894: E/AndroidRuntime(974):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-01 09:57:48.894: E/AndroidRuntime(974):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-01 09:57:48.894: E/AndroidRuntime(974):  at dalvik.system.NativeStart.main(Native Method)
05-01 09:57:48.894: E/AndroidRuntime(974): Caused by: java.lang.NullPointerException
05-01 09:57:48.894: E/AndroidRuntime(974):  at rcahmw.prototype.crowdsnapcymru.SnapCamera.onActivityResult(SnapCamera.java:84)
05-01 09:57:48.894: E/AndroidRuntime(974):  at android.app.Activity.dispatchActivityResult(Activity.java:5293)
05-01 09:57:48.894: E/AndroidRuntime(974):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)

任何建议都会很棒。在Android中使用Camera应用程序很新。

2 个答案:

答案 0 :(得分:3)

将fileUri变量保存在某处并在onActivityResult中使用它而不是data.getData

答案 1 :(得分:1)

尝试将以下文件URI传递为EXTRA_OUTPUT,如下所示。您的onCreate看起来像是:

Uri picUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_snap_camera);
    String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";  
    File imageFile = new File(imageFilePath); 
    picUri = Uri.fromFile(imageFile); // convert path to Uri        
    intent.putExtra( MediaStore.EXTRA_OUTPUT,  picUri );
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

然后onActivityResult使用picUri访问图片。将picUri声明为活动中的实例变量。因此,它将在onActivityResult

中提供