-1

In my app, the user has the option of selecting an image from the photo gallery or by taking a photo with the camera. However, when the user selects a photo to use (when they tap on it), the app crashes and this error comes up in the log -

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/46/ORIGINAL/NONE/859067274 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F46/ORIGINAL/NONE/859067274} }} to activity {com.veterinarysedations.georgeberdovskiy.veterinarysedations/com.veterinarysedations.georgeberdovskiy.veterinarysedations.DogDetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference

Here is my code -

 public void PickImage(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final String[] weightUnitOptions = {"Camera", "Gallery"};
    builder.setTitle("Pick Image from")
            .setItems(weightUnitOptions, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(takePicture, 0);
                    } else {
                        Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(pickPhoto , 1);
                    }
                }

            });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();
}

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    if (resultCode != RESULT_CANCELED) {
        if (requestCode == 0) {
            Uri selectedImage = imageReturnedIntent.getData();
            imageView.setImageURI(selectedImage);
        } else if (requestCode == 1) {
            Uri selectedImage = imageReturnedIntent.getData();
            imageView.setImageURI(selectedImage);
        } else {
            super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        }
    }
}

Here is one of the answers on StackOverflow that I tried using -

Does anybody know what I need to do to fix this error? Thanks in advance!

ADM
  • 20,406
  • 11
  • 52
  • 83
George B
  • 918
  • 3
  • 15
  • 24
  • 1
    The actual issue is the `NullPointerException` at the end of that log line. `imageView` is null in `onActivityResult()`. – Mike M. Jul 19 '18 at 04:57
  • There must be some Exception . Follow [This](https://developer.android.com/training/camera/photobasics) Use `MediaStore.EXTRA_OUTPUT` for full size image. – ADM Jul 19 '18 at 04:59
  • have you properly initialized your imageView object ? – Tejas Pandya Jul 19 '18 at 05:02

2 Answers2

2

You can try this method for getting image from camera:-

    File destination;

    public void cameraIntent() {
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    try {
        Uri mImageCaptureUri;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            destination = new File(Environment
                .getExternalStorageDirectory(),"IMG_"+System.currentTimeMillis() + ".jpg");
            mImageCaptureUri = Uri.fromFile(destination);
        } else {
            destination = new File(getFilesDir(), "IMG_" + System.currentTimeMillis() + ".jpg");
            mImageCaptureUri = Uri.parse("content://<Your package name or Directory name where you want to store>/");
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                mImageCaptureUri);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 0);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }
}

and add this line in your onActivityResult Mehtod:-

       if (requestCode == 0) {
            Uri selectedImageUri = Uri.fromFile(destination);
        }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
nik
  • 345
  • 1
  • 9
0

It turns out the problem was very simple, and was actually answered in the comments - I did not pay enough attention to the error, and failed to notice that the null object reference mentioned at the end was indeed the ImageView, which I forgot to initialize. However, I would like to thank the user who answered the question anyways - it may be helpful for others who have a similar problems on this topic.

George B
  • 918
  • 3
  • 15
  • 24