ACTION_IMAGE CAPTURE 传递结果时出现异常(从第三方应用程序拍摄照片)

发布于 2024-12-18 15:39:43 字数 5634 浏览 2 评论 0原文

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

为此,有两个按钮。画廊挑选的人工作得很好。

问题在于让你拍照的那个。点击后,成功打开相机。然后,你可以拍照,它会问你是否要保留它或丢弃它。

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

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

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

    //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);  
        }
    });

这是获取响应的活动结果方法:

@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;
        }
    }

我不知道错误来自哪里。我按照教程执行此操作,它们看起来与我的代码相同。因此,任何帮助将不胜感激。

谢谢。

My app has a form where it is required to either select a picture from the gallery or to take a photo.

For that purpose, there are two buttons. The one doing the gallery pick is working fine.

The problem comes with the one that lets you take a photo. When clicked, it successfully opens the camera. Then, you can take a picture and it will ask you wether you want to keep it or discard it.

If you choose to save it, it will try to get back to the app but it will crash.

The logcat prompts this error.

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

This is the button listener that starts the 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);  
        }
    });

And this is the Activity result method that takes the response:

@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;
        }
    }

I don't see where the error is coming from. I followed the tutorials to do this and they look the same as my code. So any help would be appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘世孤行 2024-12-25 15:39:43

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

Assuming you initialised mImg somewhere else (since you try to call setText on it), try checking if selectedImage is null. The fact that getData() would return null was a known bug on some devices, and I'm not sure it's been fixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文