ACTION_IMAGE CAPTURE(从第三方应用拍照)的异常交付结果

时间:2011-11-30 22:39:00

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

我的应用有一个表单,需要从图库中选择图片或拍照。

为此目的,有两个按钮。做画廊选择的人工作正常。

问题在于可以让你拍照的问题。单击时,它会成功打开相机。然后,你可以拍照,它会问你想要保留它还是丢弃它。

如果您选择保存它,它会尝试返回应用程序但它会崩溃。

logcat会提示此错误。

11-30 23:20:06.288: ERROR/AndroidRuntime(3045): FATAL EXCEPTION: main
11-30 23:20:06.288: ERROR/AndroidRuntime(3045): java.lang.RuntimeException: Unable to resume activity {com.android.upvar/com.android.upvar.NewPOI}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.android.upvar/com.android.upvar.NewPOI}: java.lang.NullPointerException
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.os.Looper.loop(Looper.java:130)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.main(ActivityThread.java:3687)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at java.lang.reflect.Method.invoke(Method.java:507)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at dalvik.system.NativeStart.main(Native Method)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.android.upvar/com.android.upvar.NewPOI}: java.lang.NullPointerException
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     ... 12 more
11-30 23:20:06.288: ERROR/AndroidRuntime(3045): Caused by: java.lang.NullPointerException
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at com.android.upvar.NewPOI.onActivityResult(NewPOI.java:130)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     ... 13 more

这是启动Activity的按钮侦听器:

    //mTakeImg is the button
    mTakeImg.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
        }
    });

这是采取响应的Activity结果方法:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
             super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) { 
            case ACTIVITY_SELECT_IMAGE:
                if(resultCode == RESULT_OK){  
                    Uri selectedImage = imageReturnedIntent.getData();
                    mImg.setText(selectedImage.getPath());
                    InputStream imageStream;
                    try {
                        imageStream = getContentResolver().openInputStream(selectedImage);
                        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                        mImage.setImageBitmap(yourSelectedImage);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   
                }
                break;
            case CAMERA_PIC_REQUEST:
                if (resultCode == RESULT_OK){
                    Uri selectedImage = imageReturnedIntent.getData();
                    mImg.setText(selectedImage.getPath());
                    InputStream imageStream;
                    try {
                        imageStream = getContentResolver().openInputStream(selectedImage);
                        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                        mImage.setImageBitmap(yourSelectedImage);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   
                }
                break;
        }
    }

我没有看到错误的来源。我按照教程执行此操作,它们与我的代码看起来相同。所以任何帮助都会受到赞赏。

感谢。

1 个答案:

答案 0 :(得分:1)

假设您在其他地方初始化了mImg(因为您尝试在其上调用setText),请尝试检查selectedImage是否为null。 getData()将返回null的事实是某些设备上的已知错误,我不确定它是否已被修复。