关于onActivityResult的NullPointer

时间:2013-11-29 09:13:03

标签: android android-camera android-camera-intent

使用相机,我有一个选项,让用户可以拍摄照片以及浏览他们的图库中的照片。然后将图像加载到应用程序屏幕中。我目前遇到的问题是拍照选项。

“拍照”选项适用于KitKat的设备,但不会是灾难。

代码如下:

takePhoto.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // file to save the image
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        // set the image file name
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        startActivityForResult(intent, TAKE_PICTURE);

    }

getOutputMediaFileUri函数类似于the code here.

onActivityResult()方法:

if (requestCode == TAKE_PICTURE) {
    if (resultCode == activity.RESULT_OK) {             
        String path = fileUri.getPath().toString(); 
        previewImage(path);
        try {
            getPictureDetails(path);
        } catch (FileNotFoundException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

错误日志:

11-29 14:42:12.750: E/AndroidRuntime(18570): FATAL EXCEPTION: main
11-29 14:42:12.750: E/AndroidRuntime(18570): java.lang.RuntimeException: Unable to resume activity {com.developmentcheck.dcforpublic/com.developmentcheck.dcforpublic.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=262344, result=-1, data=null} to activity {com.developmentcheck.dcforpublic/com.developmentcheck.dcforpublic.MainActivity}: java.lang.NullPointerException
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2658)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2686)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2159)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3595)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.access$800(ActivityThread.java:146)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1266)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.os.Looper.loop(Looper.java:137)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.main(ActivityThread.java:4949)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at java.lang.reflect.Method.invokeNative(Native Method)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at java.lang.reflect.Method.invoke(Method.java:511)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at dalvik.system.NativeStart.main(Native Method)
11-29 14:42:12.750: E/AndroidRuntime(18570): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=262344, result=-1, data=null} to activity {com.developmentcheck.dcforpublic/com.developmentcheck.dcforpublic.MainActivity}: java.lang.NullPointerException
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3224)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2645)
11-29 14:42:12.750: E/AndroidRuntime(18570):    ... 13 more
11-29 14:42:12.750: E/AndroidRuntime(18570): Caused by: java.lang.NullPointerException
11-29 14:42:12.750: E/AndroidRuntime(18570):    at com.developmentcheck.dcforpublic.Fragment_comment.onActivityResult(Fragment_comment.java:358)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.Activity.dispatchActivityResult(Activity.java:5369)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3220)
11-29 14:42:12.750: E/AndroidRuntime(18570):    ... 14 more

错误发生在以下行:

String path = fileUri.getPath().toString(); 

2 个答案:

答案 0 :(得分:0)

在你的onActivityResult中,首先从返回的数据意图中获取fileUri

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
    if (resultCode == activity.RESULT_OK) {             
 fileUri =data.getExtras(MediaStore.EXTRA_OUTPUT);       
String path = fileUri.getPath().toString(); 
        previewImage(path);
        try {
            getPictureDetails(path);
        } catch (FileNotFoundException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

     }

答案 1 :(得分:0)

更改您的fileUri
 fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

            String fileName = "temp.jpg";  
            ContentValues values = new ContentValues();  
            values.put(MediaStore.Images.Media.TITLE, fileName);  
            fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);