如何在Android中更改方向时检索捕获的图像?

时间:2017-05-22 13:23:05

标签: android

我有一个代码,用于在Activity中使用Camera捕获图像并在Image View中显示它。它适用于大多数设备,但是当涉及到三星时,一旦我旋转设备,图像就会消失。我应该怎么做才能在方向或锁定方向检索拍摄图像的状态

private static final int CAMERA_REQUEST = 1888;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            //Bitmap photo = ImageUtils.getInstant().getCompressedBitmap("data");
            imageView.setImageBitmap(photo);
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以检查屏幕的方向

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

     imageView.setImageBitmap(photo);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

    }
}