8

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.

Janusz
  • 187,060
  • 113
  • 301
  • 369
Jack Sam
  • 83
  • 1
  • 1
  • 5

2 Answers2

11

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.

typo.pl
  • 8,812
  • 2
  • 28
  • 29
  • I try to "remember the filename" but my member variable is also null when ActivityForResult is called... – ogborstad Dec 20 '11 at 06:14
  • @ogborstad declare your variable as a static field and DO NOT initialize to null, otherwise the variable is overwritten. – Moog Jan 15 '12 at 18:21
  • I know it's been a while since this question was posted but do you know how I would go about doing this for getting an image from the picture gallery? Apparently on a small number of phones `Uri uri = data.getData();` always returns null(S3 mini, W8L..something). – Sebek Jul 14 '14 at 11:39
  • 1
    @Sebek : How to solve this problem `data.getData() always return null` on Galaxy S3, please tell me how? – Huy Tower Sep 01 '14 at 16:08
  • @AlexTran: try using `Intent pickImageIntent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); if (pickImageIntent.resolveActivity(getPackageManager()) != null) startActivityForResult(pickImageIntent, R.id.SELECT_IMAGE_ACTIVITY_REQUEST_CODE);` – Sebek Sep 02 '14 at 09:00
  • It works for me, but it apparently restricts the number of suggested apps for browsing the gallery. Hope that helps. – Sebek Sep 02 '14 at 09:02
  • Make sure you use savedInstanceState() as suggested by @Divide else if your camera fetches very large images, you will get null as your url. – Darpan Jul 21 '15 at 09:16
3

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).

Divide
  • 540
  • 5
  • 12
  • Can you give me an example of how to handle savedInstanceState() and onRestoreInstanceState() in above example? – Darpan Jul 21 '15 at 08:56