ImageView not retaining Image when Screen rotation occurs

后端 未结 5 1172
清酒与你
清酒与你 2021-01-04 18:01
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.ut         


        
相关标签:
5条回答
  • 2021-01-04 18:22

    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

    0 讨论(0)
  • 2021-01-04 18:27

    Instead of returning the DrawingCache which you did not even build (btw), return the Bitmap


    Implementation:

    In your onActivityResult, keep 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);
    
    }
    

    In your configuration saving, save this bitmap:

    @Override
    @Deprecated
    public Object onRetainNonConfigurationInstance() {
        return bTemp;
    }
    
    0 讨论(0)
  • 2021-01-04 18:28

    getLastNonConfigurationInstance() returns an activity instance. You can fetch the previous value from that instance:

    in your 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 讨论(0)
  • 2021-01-04 18:35

    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);
         }
    }
    
    0 讨论(0)
  • 2021-01-04 18:45

    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

    0 讨论(0)
提交回复
热议问题