简体   繁体   中英

ImageView crop image disappear when change orientation

I have an activity where I take a picture, crop the image and then show the cropped image. here is my code.

public class MainActivity extends Activity {
    private static final int TakePicture = 1;
    ImageView imgview;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgview = (ImageView) findViewById(R.id.imageView1);
        Button buttonCamera = (Button) findViewById(R.id.btn_take_camera);
        buttonCamera.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // call android default camera
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
                // ******** code for crop image
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 0);
                intent.putExtra("aspectY", 0);
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 150);
                try {
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent, TakePicture);
                } catch (ActivityNotFoundException e) {
                    // Do nothing for now
                }
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              Bundle extras = data.getExtras();
            if (extras != null) {
                Bitmap photo = extras.getParcelable("data");
                imgview.setImageBitmap(photo);
            }
    }

}

when i change the orientation from portrait to landscape, the image set in bitmap will disappear. Is there a way to save the current state of the image even if the orientation is changed from landscape to portrait or vice versa.?

I have added the method that saves the current state of the app..

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    image = savedInstanceState.getParcelable("BitmapImage");
    this.photo = image;
    imageView.setImageBitmap(photo);
    super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState (Bundle savedInstanceState) {
    savedInstanceState.putParcelable("BitmapImage", photo);
    super.onSaveInstanceState(savedInstanceState);
}

i hope this will help others..

You don't have a method that saves the current state of your app. Use onSaveInstanceState in order to save. You can find here everything about activity and saving values: http://developer.android.com/reference/android/app/Activity.html

Kind regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM