打开相机意图时如何防止Android应用程序方向

时间:2016-04-03 19:37:42

标签: android android-intent bitmap

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

  


机器人:screenOrientation ="肖像"
  机器人:configChanges =" keyboardHidden |取向"

我也试过使用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));


    }
}
}

感谢。

1 个答案:

答案 0 :(得分:1)

  

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

您不仅需要处理配置更改(例如方向更改),还需要处理在摄像头应用程序位于前台时终止进程的情况。这种情况比您预期的更频繁。

  

我也试过使用onSaveInstanceState但没有任何作用

嗯,你采取的方法可能不起作用。我不认为你可以从Bitmap退出ImageView。更重要的是,保存的实例状态需要小于1MB,并且三个照片位图可能超过该位。

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