方向改变后保持图像视图

keep imageview after orient change

我从相机或画廊获取图像并将其设置到我的 imageview。不幸的是,当屏幕旋转时,图像视图什么也不显示。 如何让图像保持可见?

这是我获取图像的代码:

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

    switch (requestCode){
        case REQUEST_IMAGE_CAPTURE:

    // if taking
    if (resultCode == RESULT_OK && null != data  && TAKE_OR_PICK == 1) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");

        int width = imageBitmap.getWidth();
        int height = imageBitmap.getHeight();
        if(height>width) {
            int crop = (height - width) / 2;
            Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, width, width);
            image.setImageBitmap(cropImg);
        }
        else if (width>height){int crop = (width - height) / 2;
            Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, height, height);
            image.setImageBitmap(cropImg);}
        else {image.setImageBitmap(imageBitmap);}

    }
        break;

        case RESULT_LOAD_IMAGE:
        // if choosing
    if (resultCode == RESULT_OK && null != data  && TAKE_OR_PICK == 2) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        Bitmap imageBitmap = BitmapFactory.decodeFile(picturePath);
        int width = imageBitmap.getWidth();
        int height = imageBitmap.getHeight();
        if(height>width) {
            int crop = (height - width) / 2;
            Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, width, width);
            image.setImageBitmap(cropImg);
        }
        else if (width>height){int crop = (width - height) / 2;
            Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, height, height);
            image.setImageBitmap(cropImg);
        }
        else {image.setImageBitmap(imageBitmap);}

    }

我试过使用 onSaveInstanceState() 但无法让它用于位图。还有其他方法可以实现吗?

您的情况是,如果您旋转设备,activity 是 recreated.Try 在 AndroidManifest.xml 文件的 activity 标签内添加以下内容。希望这有帮助。

android:configChanges="orientation | screenSize"

一般有以下三种方式:

  1. 正如一些答案所建议的那样,您可以区分以下情况 您的 activity 是第一次创建并正在恢复中
    来自保存的实例状态。这是通过覆盖
    onSaveInstanceState 并检查 onCreate 的参数。

    2.You 可以通过添加 android:screenOrientation="portrait"(或 "landscape")将 activity 锁定在一个方向 在你的清单中。

    3.You 可以通过指定
    告诉系统您打算自己处理屏幕更改 android:configChanges="orientation" 在标签中。这条路 activity 不会重新创建,但会收到回调
    相反(你可以忽略它,因为它对你没有用)

正如 Gazi 所说,如果不允许用户旋转屏幕,"close" 通过清单的选项。

如果它不相关,你应该有一个函数

fillAllData()

"onResume" 和函数内部调用,如果您将位图添加到图像中(您可以将其与 onSaveInstance 一起保存)

    private ImageView mMyImageView;
    private String tempFileName;

    onCreate(){
      setContentView(R.layout.myLayout);
      mMyImageView = (ImageView)findViewById(R.id.myImageView);
    }

    onActivityResult(){
       //do all your code plus this:
      tempFileName = random string;
      save bitmap after process in tempFileName
      update in application some flag that a temp file image is available;
    }

    onResume(){
      if temp file name is available load bitmap into your image view.
    }