此外,我尝试使用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));
}
}
}
多谢了。
当我有意打开相机时,屏幕会旋转,然后所有的位图都消失了
您不仅需要处理配置更改(例如方向更改),还需要处理当相机应用程序处于前景时进程被终止的情况。这种情况发生的频率比您可能预料的要高。
我也试过使用onSaveInstanceState,但没有效果
我创建了一个主题为“导航抽屉活动”的项目,我想在左侧菜单上有两个选项: null
我正在创建一个只打算在肖像模式下运行的应用程序。在应用程序中,我使用一个意图调用Android原生相机应用程序。当手机处于纵向方向时,应用程序按预期工作。当手机处于横向方向时,相机应用程序以横向方向启动,这导致当相机意图关闭时,主activity和我的片段被重新创建,这导致一些无意的行为。 有没有办法强制相机应用在通过意图推出时只能以人像方向打开? 谢谢,
我的应用程序在Android 7.0中打开相机时崩溃
我正在使用Camera2 API创建自定义相机。我想在使用相机预览保存图像后重新打开相机(继续拍摄图像)。我试图在保存图像后重新打开相机,但显示错误。 android.view.ViewRootImpl$CalledFromWrongThread异常:只有创建视图层次结构的原始线程可以触摸其视图。 开放式摄像法 重新打开相机方法 捕获会话 保存图像方法