1

I'm having a problem while testing my app on a friends Galaxy S4 (GT i9505, Android 5.1). When giving a file URI to camera intent, OnActivityResult gives result Activity.RESULT_OK and and the path is null. It is working on most of other devices I tested (LG G3, nexus 5...). This is my code:

GetOutputMediaFile

public File getOutputMediaFile(int type){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), MediaChooserConstants.folderName);
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MediaChooserConstants.MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if(type == MediaChooserConstants.MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }
    return mediaFile;
}

OnActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        String picturePath = null;
        if (requestCode == MediaChooserConstants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            picturePath = fileUri.getPath(); // <--- fileURI is null
        }
    }
}

DispatchTakePhotoIntent

private void dispatchTakePictureIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File file = Utils.getInstance().getOutputMediaFile(MediaChooserConstants.MEDIA_TYPE_IMAGE);

    fileUri = Uri.fromFile(file); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    //fileUri is not null here while debugging (../DCIM/.../IMG_XXX.jpg)
    startActivityForResult(intent, MediaChooserConstants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}

2 Answers2

2

you need to save the file path to the bundle in onSaveInstanceState and then get it again in onRestoreInstanceState from the bundle

tyczj
  • 71,600
  • 54
  • 194
  • 296
2

Save path of image like this :

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {

    if(mImageCaptureUri!=null)
        savedInstanceState.putString("camera_image", mImageCaptureUri.toString());

    super.onSaveInstanceState(savedInstanceState);
}

And Retrieve image path from this :

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {

    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey("camera_image")) {
            mImageCaptureUri = Uri.parse(savedInstanceState.getString("camera_image"));
        }
    }

    super.onRestoreInstanceState(savedInstanceState);
}

This problem happens, Only when user goes to camera intent and by the time he captures the image the activity on which camera intent was hosted gets destroyed or recreated when user comes back from the camera intent.

Vipul Asri
  • 8,903
  • 3
  • 46
  • 71
  • I'll try it out, but you made a bad copy/paste in `onRestoreInstanceState` by putting the `URI` to InstanceState again. And just a reminder to anyone else, since I'm doing this in a fragment (didn't mention it before), fragments have `onActivityCreated` instead of `onRestoreInstanceState`. – Lukas Stančikas Sep 23 '15 at 08:05
  • i have edited my answer according to Activity. And as you said you didn't mentioned "Fragments" earlier and also you are right fragments `onActivityCreated` instead of `onRestoreInstanceState`. – Vipul Asri Sep 23 '15 at 08:19
  • BTW, i can't test on my friends device at the moment, but on my device (which is working without resetting the uri to null), `savedInstanceState` is called, but `onActivityCreated` is not called. Is it a normal behaviour? – Lukas Stančikas Sep 23 '15 at 08:51
  • yeah sometimes its normal, as recreation of activity depends on the phone performance. If you test this thing with Nexus 6 it will work fine without recreation but if you test it on Moto G2. it will call `onRestoreInstanceState` or as in your case `onActivityCreated` because of memory and performance issues. – Vipul Asri Sep 23 '15 at 09:03