Home » Android » Android Samsung: Camera app won't return intent.getData()

Android Samsung: Camera app won't return intent.getData()

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am developing an app, where an image taken from the native camera app. is to be shown to the user.
The code I did is:

/* Intent */
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);

/* Result */
onActivityResult(int requestCode, int resultCode,
        Intent returnIntent) {
    if(requestCode == TAKE_PICTURE) {
        //picture from Camera
        if (resultCode == Activity.RESULT_OK) {
            if(returnIntent != null) {
                try {
                    // Get the file path where the image is stored.
                    // This runs fine on all devices, except Samsung ones.
                    Uri selectedImage = returnIntent.getData();

                    if(selectedImage == null) {
                        if(returnIntent.getExtras() != null &&
                            returnIntent.getExtras().get(AppConstants.DATA) != null) {

                            // But, I get the image Bitmap here. Means the image is stored in disk.
                            Bitmap bmp = (Bitmap) returnIntent.getExtras().get(AppConstants.DATA);
                        }
                    }
                } catch (Exception e) {
                    //
                }
            }
        }
    }
}

The problem here is, the above code works fine on all devices I tried (HTC’s, SE’s) but it somehow fails in the Samsung ones. The “Uri selectedImage = returnIntent.getData();” never returns anything. As my entire app is built over this logic of file path storing, I am not able to proceed.
Is there any solution people.

How to&Answers:

See Specify filename for picture to be taken for code to specify an EXTRA_OUTPUT parameter for the Intent which lets you specify a filename for the picture. Remember the filename when the activity result is called and use that if the intent.getData is NULL

And if you read the other comments in that bug report, you’ll realize how many problems that picture taking on Android has.

Answer:

Your best bet is to generate a file path you want to save the picture to and store it in a member variable. Then, when calling camera activity, put it (as Uri) in the MediaStore.EXTRA_OUTPUT extra.

Be sure to override onSaveInstanceState() and onRestoreInstanceState() and save/restore this path properly. This will make sure you’ll still have it if the system decides to restart your activity in the meantime (which is very possible due to orientation changes and/or out of memory conditions).