2

I am writing a simple app that set a image to a ImageView through an Intent calling the camera. But when I change the orientation the image disappears. I've already tried this questions Q1, Q2. The previous questions suggest either use onSaveInstanceState or Retaining an Object During a Configuration Change.

But in the Android docs I've read:

you might encounter a situation in which restarting your application and restoring significant amounts of data can be costly and create a poor user experience

So, if a Bitmap is consider to be a “significant amount of data” I will need to use setRetainInstance, but in the docs says:

While you can store any object, you should never pass an object that is tied to the Activity, such as a Drawable, an Adapter, a View or any other object that's associated with a Context

What approach should I use to be able to restore the Image on a orientation change?

What I've done so far

private ImageView mPhotoImgage;
private Bitmap mCameraData;

onCreate {
    mPhotoImgage = (ImageView) findViewById(R.id.photoResultImage);

    if (savedInstanceState != null){
        mPhotoImgage.setImageBitmap((Bitmap)savedInstanceState.getParcelable(IMAGE_RESOURCE));
    }
}

onResume{
    if (mCameraData != null){
        mPhotoImgage.setImageBitmap(mCameraData);
    }
    super.onResume();
}

onSaveInstanceState{
    outState.putParcelable(IMAGE_RESOURCE, mCameraData);
    super.onSaveInstanceState(outState);
}

But after two orientation changes the image disappears.

Community
  • 1
  • 1
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79

2 Answers2

1

You should use the onResume method to set your image when the activity resumes

@Override
protected void onResume() {
    //your code here
    super.onResume();
}

You dont need on saved instance, just copy the code you use on onCreate to set your image to the onResume and it should work.

for example if you have on onCreate :

 imageView im.setImageResource(R.drawable.image);

Use it for onResume :

@Override
protected void onResume() {
    imageView im.setImageResource(R.drawable.image);
    super.onResume();
}
mremremre1
  • 1,036
  • 3
  • 16
  • 34
  • I am getting the image through the camera with `data.getExtras().get("data")`. – Alejandro Alcalde May 30 '14 at 23:03
  • you could try saving the image as a jpeg and loading it. I give you a link to download a simple test project of mine to see how you can capture a jpeg without opening the camera app. Use the code as you wish and modify it to do what you need http://www.4shared.com/rar/v-ZQPybcce/Test.html – mremremre1 May 30 '14 at 23:08
1

You could instead ask the camera to save the captured image to a file to external storage (if available). You tell it where to save the image, and that means you can store the file URI in onSaveInstanceState, and load it from that URI when you're restoring the state.

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(context.getExternalFilesDir(null), "randomfilename")));
startActivityForResult(cameraIntent, RC_CAMERA);

You should keep track of when the file is no longer needed and delete it.

Another advantage of this approach is that you can find out the dimensions of the image before it's been loaded into memory.

Dave Morrissey
  • 4,371
  • 1
  • 23
  • 31
  • I do not know why putting this line `putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(this.getCacheDir(), "randomfilename")))` make the camera app never return. I take a photo and the button for accept the photo and closes the camera app does nothing – Alejandro Alcalde May 30 '14 at 23:26
  • I've edited my answer to remove the suggestion to use the app cache, which is private so the camera can't write to it. It should be possible to grant permission but I failed to get that working. The code you've posted isn't complete but I think you may not be setting `mCameraData` in `onCreate`, which would explain why the image disappears on the second orientation change. – Dave Morrissey May 31 '14 at 00:45
  • I now get a NPE when retrieving the data on `onActivityResult`, should I retrieve the photo there with `mPhotoImgage.setImageURI((Uri)data.getExtras().get("data"))` – Alejandro Alcalde May 31 '14 at 07:24
  • The URI won't be returned in the intent data as far as I know (debug to confirm), you need to remember the URI you sent in the intent, and store that in the fragment or activity, restoring it after orientation change using `onSaveInstanceState`. – Dave Morrissey May 31 '14 at 07:29
  • OK, Thank you, I am going to try – Alejandro Alcalde May 31 '14 at 08:43