ImageView does not save image when rotating the screen

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 use an image to save an image that was captured using Camera Intent, but when the screen rotates, the image is lost. I tried using onRetainNonConfigurationInstance () but did not work

And I do not want to write the image to a file.

+7
android android-imageview
source share
5 answers

it can 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); } } 
+5
source share

You can avoid recreating activity by setting the orientation and screen flag in the manifest file

 android:configChanges="orientation|screenSize" 

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

+4
source share

The reason your image disappears when you rotate the screen is because the action is destroyed and recreated. During this process, the selected image is not saved, as you can read here

Warning: your activity will be destroyed and recreated every 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 may need to load alternative resources (for example, a layout).

If you want to save the selected image, you should not use the state of the instance, as you can read here .

Bind that the system saves the onSaveInstanceState () callback for you, which is not intended to transfer large objects (for example, bitmaps), and the data inside it must be serialized and then deserialized, which can consume a lot of memory and make the configuration change slow. In such a situation, you can ease the burden of reinitializing your activity by saving a snippet when your activity is restarted due to a configuration change. This snippet may contain references to stateful objects that you want to save.

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

+2
source share

getLastNonConfigurationInstance() returns an instance of activity. You can get the previous value from this instance:

in onCreate() :

 YourActivity prevActivity = (YourActivity) getLastNonConfigurationInstance(); if(prevActivity!= null) { this.bTemp = prevActivity.bTemp; } 

and your onRetainNonConfigurationInstance() method should be:

 @Override @Deprecated public Object onRetainNonConfigurationInstance() { return bTemp; } 

and your onActivityResult () method should be:

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

Instead of returning a DrawingCache that you didn’t even create (btw), return Bitmap


Implementation:

In your onActivityResult save the bitmap in bTemp:

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

When saving the configuration, save this bitmap:

 @Override @Deprecated public Object onRetainNonConfigurationInstance() { return bTemp; } 
0
source share

All Articles