Search code examples
javaandroidandroid-cameraandroid-camera-intent

Android "Taking Photos Simply" tutorial does not work for me


So, I am trying to implement taking of photos in my app.

I am following the official "Taking Photos Simply" tutorial, but it just gives me errors.

The only thing I am doing differently, it that I am using a popup dialog fragment.

Below is the error I get:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131073, result=-1, data=null} to activity {com.myapp.demo/com.myapp.demo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3605)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3648)
        at android.app.ActivityThread.access$1400(ActivityThread.java:154)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5289)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.paulcanning.mote.NoteDialogFragment.onActivityResult(NoteDialogFragment.java:179)
        at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:153)
        at android.app.Activity.dispatchActivityResult(Activity.java:6192)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3601)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3648)
            at android.app.ActivityThread.access$1400(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

Button click listener

takePhoto.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
                //startActivityForResult(takePictureIntent, 1);

                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(photoFile));
                    startActivityForResult(takePictureIntent, 1);
                }
            }
        }

    });

onActivityResult (note, it wouldn't let me use protected)

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == getActivity().RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        imageView.setImageBitmap(imageBitmap);
    }
}

Solution

  • You are supplying EXTRA_OUTPUT. Hence, camera apps do not need to supply getExtra("data"), so it will generally be null.

    Quoting the documentation for ACTION_IMAGE_CAPTURE:

    The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

    Your image (hopefully) is in the location that you supplied in EXTRA_OUTPUT.