Android:使用Camera Intent时,应用程序在onActivityResult上崩溃 [英] Android: App crashes on onActivityResult while using Camera Intent

查看:85
本文介绍了Android:使用Camera Intent时,应用程序在onActivityResult上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用相机意图在我的App中捕获图像.使用相机时,我的应用程序在Android 5.0.2上崩溃的问题.我正在使用片段中的意图.下面是我的代码片段:

I am using camera intent to capture images in my App. The problem my app crashes on Android 5.0.2 while using camera. I am using intent from fragment. Below is my code inside fragment:

拍照方法

private void takePhoto() {
    mHighQualityImageUri = Util.generateTimeStampPhotoFileUri(getActivity());
    Log.d(UploadPicturesFragment.class.getSimpleName(),
            "URI: " + mHighQualityImageUri.toString());
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mHighQualityImageUri);
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}

onActivityResult在我的片段中

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
        return;
    }

    if (requestCode == REQUEST_IMAGE_CAPTURE) {
    Log.d(UploadPicturesFragment.class.getSimpleName(),
                "IMAGE URI NOT NULL: " + (mHighQualityImageUri == null));
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),
                    mHighQualityImageUri);
            DroomUtil.beginCrop(mHighQualityImageUri, getActivity(), this, true, bitmap.getWidth(),
                    bitmap.getHeight());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

日志跟踪

12-29 10:28:03.491: E/AndroidRuntime(9780): java.lang.RuntimeException:       Unable to resume activity {in.droom/in.droom.activity.MainActivity}:     java.lang.RuntimeException: Failure delivering result    ResultInfo{who=android:fragment:2, request=1, result=-1, data=null} to activity   {in.droom/in.droom.activity.MainActivity}: java.lang.NullPointerException:   Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()'   on a null object reference
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3224)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3257)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2479)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.os.Looper.loop(Looper.java:155)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.main(ActivityThread.java:5702)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at java.lang.reflect.Method.invoke(Native Method)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at java.lang.reflect.Method.invoke(Method.java:372)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
12-29 10:28:03.491: E/AndroidRuntime(9780): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:2, request=1, result=-1, data=null} to activity {in.droom/in.droom.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3881)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3197)
12-29 10:28:03.491: E/AndroidRuntime(9780):     ... 11 more
12-29 10:28:03.491: E/AndroidRuntime(9780): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.content.ContentResolver.openInputStream(ContentResolver.java:651)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:1019)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at in.droom.fragments.UploadPicturesFragment.onActivityResult(UploadPicturesFragment.java:395)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.Activity.dispatchActivityResult(Activity.java:6164)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3877)
12-29 10:28:03.491: E/AndroidRuntime(9780):     ... 12 more

行号395是:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),
                    mHighQualityImageUri);

推荐答案

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference

这意味着mHighQualityImageUri很可能是null.如果您无法使用onSaveInstanceState()抓住该Uri,则会发生这种情况.当您的应用程序在后台,而相机应用程序在前台时,您的进程很可能会终止.

That means mHighQualityImageUri is null, in all likelihood. This will occur if you failed to hold onto that Uri using onSaveInstanceState(). It is entirely possible that your process will be terminated while your app is in the background and the camera app is in the foreground.

这篇关于Android:使用Camera Intent时,应用程序在onActivityResult上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆