Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView not retaining Image when Screen rotation occurs

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    ImageView iv;
    Bitmap bTemp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        Button cameraClick = (Button) findViewById(R.id.click);
        iv = (ImageView) findViewById(R.id.imageView1);

        final Bitmap data = (Bitmap) getLastNonConfigurationInstance();
        if (data == null) { 

           iv.setImageBitmap(bTemp);
        }


        cameraClick.setOnClickListener(myhandler);

    }


    OnClickListener myhandler = new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 0);
        }
      };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap bm = (Bitmap) data.getExtras().get("data");
        iv.setImageBitmap(bm);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }




    @Override
    @Deprecated
    public Object onRetainNonConfigurationInstance() {
        bTemp = iv.getDrawingCache();
        return bTemp;
    }


}

I'm using a imageview to store the image which is captured using the Camera Intent but when the screen rotates the image is lost. I tried using onRetainNonConfigurationInstance() but its not working

And i don't want to write the image to afile.

like image 684
oomkiller Avatar asked Nov 08 '13 10:11

oomkiller


People also ask

How do you keep an image in an android activity during orientation change?

the recommended way is to use setRetainInstance(true) in the fragment to retain images. Runtime configuration changes, such as a screen orientation change, cause Android to destroy and restart the running activity with the new configuration (For more information about this behavior, see Handling Runtime Changes).

How do you handle a dysfunction in screen reorientation?

Prevent Activity to recreated Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

How to handle screen rotation in android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

What happens when screen orientation changes in android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.


3 Answers

You can avoid recreating the activity by setting the orientation and screensize flag in your Manifest file

android:configChanges="keyboard|orientation|screenSize"

If needed you can implement onConfigurationChanged() which will be called when the orientation changes. More information is in http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

like image 160
Bharath Kumar Avatar answered Sep 29 '22 01:09

Bharath Kumar


this may help you...

@Override
protected void onSaveInstanceState(Bundle outState) {
    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    outState.putParcelable("image", bitmap);
    super.onSaveInstanceState(outState);
}

protected void onCreate(Bundle savedInstanceState) {
     if(savedInstanceState != null) {
        Bitmap bitmap = savedInstanceState.getParcelable("image");
        imageView.setImageBitmap(bitmap);
     }
}
like image 37
Gopal Gopi Avatar answered Sep 28 '22 01:09

Gopal Gopi


The reason why your image disappears when screen rotates is that the activity is destroyed and recreated. During such process, the selected image is not retained as you can read here

Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

If you want to retain the selected image you should not use Instance State as you can read here.

Bundle that the system saves for you with the onSaveInstanceState() callback—it is not designed to carry large objects (such as bitmaps) and the data within it must be serialized then deserialized, which can consume a lot of memory and make the configuration change slow. In such a situation, you can alleviate the burden of reinitializing your activity by retaining a Fragment when your activity is restarted due to a configuration change. This fragment can contain references to stateful objects that you want to retain.

I implemented this solution and you can find the code in my answer to this other question

like image 20
ilmatte Avatar answered Oct 02 '22 01:10

ilmatte