Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: App crashes on onActivityResult while using Camera Intent

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:

Method to take photo

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 in my fragment

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

Log Trace

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

The line no. 395 is:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),
                    mHighQualityImageUri);
like image 989
Nitish Avatar asked Dec 29 '15 05:12

Nitish


1 Answers

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

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.

like image 183
CommonsWare Avatar answered Sep 29 '22 17:09

CommonsWare