DeadJohnDoe DeadJohnDoe - 3 months ago 13
Android Question

How to keep an image in an Android activity during an orientation change?

I am very new to Android programming and have been experimenting with building an app. In my new app I have the user press a button which allows them to choose an app on their device such as 'Gallery' in order to select an image. This image is then displayed in an imageview window on the activity screen. Unfortunately, when I turn my phone and the orientation changes, the image is lost and they have to go select it again.

How do I save the bitmap image and have it re-displayed after the orientation change?

Here is the code function I use to let the user pick an image app, pick an image, and display the image in the imageview window.

@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
textTargetUri.setText(targetUri.toString());
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
targetImage.setImageBitmap(bitmap);
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
}


The above function is initialized within my onCreate in response to a button click shown in this code:

Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
textTargetUri = (TextView)findViewById(R.id.targeturi);
targetImage = (ImageView)findViewById(R.id.targetimage);

buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}});


Essentially, I just need to put this boolean in my onCreate and have it put the image back in the window. It also displays text showing where the image was located on the device but that is not as important to me as being able to have the image stay on the screen.

if (savedInstanceState != null) {


}


But it seems to wipe the data after the orientation change?
I have no idea how to save the data and restore it to be used within that boolean.
I appreciate any advice, code, or links you can provide!




UPDATE:

Thank you Vishal Vijay and rajahsekar for your quick and helpful responses. Using both of your advised coding as well as some code I had found from previous research, I was able to compile a solution. However, while it solved the first issue, it spawned another which I will describe after showing the code I used to fix the initial problem.

I found that I had 2 options available for the restoration portion.

Either I could place this in the onCreate:

if (savedInstanceState != null) {
//if there is a bundle, use the saved image resource (if one is there)
image = savedInstanceState.getParcelable("BitmapImage");
targetImage.setImageBitmap(image);
textTargetUri.setText(savedInstanceState.getString("path_to_picture"));R.drawable.camera_button);
}


This allowed me to have both the image as well as the path to it on the device to be re-displayed on my activity.

Or I could have this after the onCreate

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
image = savedInstanceState.getParcelable("BitmapImage");
targetImage.setImageBitmap(image);
textTargetUri.setText(savedInstanceState.getString("path_to_picture"));
}


and the saving function that I used was

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelable("BitmapImage", bitmap);
savedInstanceState.putString("path_to_picture", picture_location);
}


To clarify where picture_location came from, back in my onActivityResult function I added a line and changed another to save the path string

if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
picture_location = targetUri.toString();
textTargetUri.setText(picture_location);


As for variables initialized before the onCreate,

Bitmap image;
Bitmap bitmap;
String picture_location;


Now, I realize this has gotten rather lengthy but I hope it will help others who may have a similar problem. As for the new issue that I mentioned earlier, when I rotate my android device, it does in fact keep the data and re-display it as I was trying to do. However, when I rotate the screen again, the original issue arises; the data seems to be lost and is not displayed on the screen.

I would hope there is a much more simple fix so that the save and restore functions keep working no matter how many times the screen orientation changes?

Thanks again guys!

Answer
Bitmap image;

public void onCreat(..){
    if (savedInstanceState != null) {
        image = (Bitmap) savedInstanceState.getParcelableExtra("BitmapImage");; 
    } else { 
        image = yourBitmapImage;
    } 
}

@Override public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putExtra("BitmapImage", bitmap);
}