【问题标题】:how to prevent android app orientation when opening camera intent打开相机意图时如何防止android应用程序方向
【发布时间】:2016-04-03 19:37:41
【问题描述】:

我正在制作一个简单的 android 应用程序,它可以拍摄照片并将图像视图保存为位图,问题是当我打开相机意图时屏幕旋转,然后所有位图都消失了,我尝试了很多东西,其中之一是


android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"

我也尝试使用 onSaveInstanceState 但没有任何效果,这是我的代码:

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

    //Initializing Variables
    settings = (Button) findViewById(R.id.Bsettings);
    next = (Button) findViewById(R.id.Bnext);
    takePic = (Button) findViewById(R.id.Bpic);

    imageView1 = (ImageView) findViewById(R.id.IVthumbnail1);
    imageView2 = (ImageView) findViewById(R.id.IVthumbnail2);
    imageView3 = (ImageView) findViewById(R.id.IVthumbnail3);


    // Handling event listeners
    takePic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);

        }
    });

    settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (imageView1.getDrawable() != null) {
                Toast.makeText(getApplicationContext(), "image is NOT null", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "image is null", Toast.LENGTH_SHORT).show();

            }
        }
    });
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (imageView1.getDrawable() != null) {
        bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();
        tempBitmap = bitmapDrawable.getBitmap();
        outState.putParcelable("bitmap1", tempBitmap);
    }
    if (imageView2.getDrawable() != null) {
        bitmapDrawable = (BitmapDrawable) imageView2.getDrawable();
        tempBitmap = bitmapDrawable.getBitmap();
        outState.putParcelable("bitmap2", tempBitmap);
    }
    if (imageView3.getDrawable() != null) {
        bitmapDrawable = (BitmapDrawable) imageView3.getDrawable();
        tempBitmap = bitmapDrawable.getBitmap();
        outState.putParcelable("bitmap3", tempBitmap);
    }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState != null) {

        if (savedInstanceState.getParcelable("bitmap1") != null) {
            tempBitmap = (Bitmap) savedInstanceState.getParcelable("bitmap1");
            imageView1.setImageBitmap(tempBitmap);
        }
        if (savedInstanceState.getParcelable("bitmap2") != null) {
            tempBitmap = (Bitmap) savedInstanceState.getParcelable("bitmap2");
            imageView1.setImageBitmap(tempBitmap);
        }
        if (savedInstanceState.getParcelable("bitmap3") != null) {
            tempBitmap = (Bitmap) savedInstanceState.getParcelable("bitmap3");
            imageView1.setImageBitmap(tempBitmap);
        }
    }
}

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

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        bitmap = (Bitmap) data.getExtras().get("data");
        if (imageView1.getDrawable() == null) {
            System.out.println("1");
            imageView1.setImageBitmap(bitmap);
        } else if (imageView1.getDrawable() != null && imageView2.getDrawable() == null) {
            System.out.println("2");
            bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();
            tempBitmap = bitmapDrawable.getBitmap();
            imageView2.setImageBitmap(tempBitmap);
            imageView1.setImageBitmap(bitmap);
        } else {
            System.out.println("3");

            bitmapDrawable = (BitmapDrawable) imageView2.getDrawable();
            tempBitmap = bitmapDrawable.getBitmap();
            imageView3.setImageBitmap(tempBitmap);
            bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();
            tempBitmap = bitmapDrawable.getBitmap();
            imageView2.setImageBitmap(tempBitmap);
            imageView1.setImageBitmap(bitmap);


        }
        bitmap = null;
        tempBitmap = null;
        bitmapDrawable = null;

        // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
        //  bitmapUri = getImageUri(getApplicationContext(), bitmap);
        // CALL THIS METHOD TO GET THE ACTUAL PATH
        //  bitmapPath = new File(getRealPathFromURI(bitmapUri));


    }
}
}

谢谢。

【问题讨论】:

  • 不要试图阻止方向改变。相反,您应该将进度保存到数据库或其他存储中,这样当您的应用发生配置更改时,它可以从中断的地方继续。
  • 我使用了“onSavedInstanceState”,但它不起作用。

标签: android android-intent bitmap


【解决方案1】:

当我打开相机意图时,屏幕旋转,然后所有位图都消失了

您不仅需要处理配置更改(例如方向更改),还需要处理当相机应用程序处于前台时进程终止的情况。这种情况发生的频率超出了您的预期。

我也尝试使用 onSaveInstanceState 但没有任何效果

嗯,您采取的方法可能行不通。我不认为您可以从ImageView 中取出Bitmap。更重要的是,保存的实例状态需要小于 1MB,三张照片位图可能会超过这个值。

由于您还需要处理进程终止,因此您需要将这三个位图保存到 internal storage 某处。您可以将File 对象放入保存的实例状态Bundle,然后根据需要使用您最喜欢的image-loading library 重新加载这些图像。

【讨论】:

  • 你说得对,只有共享偏好解决了我的问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多