[英]why while calling camera in android all varibles(got from Activity1 through intent ) in the activity are reinitilized?
我是android的新手,我在android中遇到相机问题。 在我的课堂上,我有一些实例变量(持有一些从Activity1通过意图获得的值)以及一个按钮和图像视图。 当我单击该按钮时,应用程序将打开相机并将其存储在SD卡中,之后,我将使用实例变量进行一些操作。 但是问题是变量被重新初始化为默认值。 我已经使用共享首选项或在另一个类中将(变量数据)存储为静态变量,那么它可以正常工作。 我的问题是为什么所有实例变量都要重新初始化。
在我的应用程序Activity1-(调用中)-> Activity2 [这有一个按钮和一个图像视图]中,这里我通过意图从Activity1传递一些数据到Activity2,在Activity2中(在Activity2中,我在调用相机之前成功获取了数据)如果正在调用相机,则将我从意图中获取的数据重新初始化为默认值。为避免这种情况,我没有传递Activity1的数据(我已将其存储在SharedPreference或常量类中作为静态变量,它可以正常工作)。这些是唯一的解决方案吗?
这是Activity2中的相机代码
// camera code
public void openCamera() {
if (Helper.checkCameraHardware(this)) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateFileName = sdf.format(new Date()); // generating
// todays date
// for folder
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
String curentDateandTime = sdf1.format(new Date()); // generating
// todays
// date with
// min,seconds
// for image
// creating an folder in sd card : ed ==> sdCard path/IMG_Folder
// in helper class / folder with todays date
File sdImageMainDirectory = new File(Environment
.getExternalStorageDirectory().getPath()
+ "/"
+ Helper.IMG_FOLDER + "/" + dateFileName);
if (!sdImageMainDirectory.exists()) { // if folder not exists it
// created
sdImageMainDirectory.mkdirs();
}
// setting image path to sd card/Img_folder in helper
// class/todays date folder
image_PATH = Environment.getExternalStorageDirectory()
.getPath()
+ "/"
+ Helper.IMG_FOLDER
+ "/"
+ dateFileName + "/";
// creating a new file(jpg) at above image path
File file = new File(image_PATH, curentDateandTime + ".jpg");
// re-initilization of image_PATH to new file(jpg)
image_PATH = image_PATH + curentDateandTime + ".jpg";
savePreferences();
// creating an uri for file
Uri outputFileUri = Uri.fromFile(file);
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i, 1234);
} catch (Exception e) {
Helper.AlertBox(this,
"Error No: 001\nPlease Contact Technical Person.\n"
+ e.toString());
}
} else {
Helper.AlertBox(this, "Camera Not Found.!");
}
}
private void savePreferences() {
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("image_PATH", image_PATH);
// Commit the edits!
editor.commit();
}
private void restorePreferences() {
SharedPreferences settings = getPreferences(MODE_PRIVATE);
image_PATH = settings.getString("image_PATH", "");
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// image_PATH = "";
image_str = "";
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1234) {
if (resultCode == RESULT_OK) {
restorePreferences();
System.out.println("image_PATH :" + image_PATH);
File file = new File(image_PATH);
if (file.exists()) {
Log.e("File exist :", "File exist at " + image_PATH);
FileInputStream in;
BufferedInputStream buf;
try {
// in = new FileInputStream(image_PATH);
// buf = new BufferedInputStream(in);
// bMap = BitmapFactory.decodeStream(buf);
// iv_image.setVisibility(View.VISIBLE);
// iv_image.setImageBitmap(bMap);
// ConstantClass.image_PATH = image_PATH;
// if (in != null) {
// in.close();
// }
// if (buf != null) {
// buf.close();
// }
intent = new Intent(TakePhoto.this, PhotoPreview.class);
intent.putExtra("index", Helper.index);
Log.e("test", "0");
Helper.bitMap[Helper.index] = image_PATH;
Log.e("test", "1");
Helper.btn[Helper.index] = false;
Log.e("test", "2");
startActivity(intent);
Log.e("test", "3");
// Helper.AlertBox(this, "Image Captured.");
} catch (Exception e) {
Helper.AlertBox(this,
"Error No: 004\nPlease contact technical person.\n"
+ e.toString());
Log.e("Error reading file", e.toString());
}
} else {
Helper.AlertBox(this,
"Error No: 005\nPlease contact technical person.");
}
} else {
Toast.makeText(getApplicationContext(), "Cam Not Supported",
5000).show();
}
}
}
// camera code end
当我单击图像按钮时,将调用openCamera()。
提前致谢
您的问题与“ 相机”或“ 共享首选项”无关。 屏幕外的任何活动都可能发生相同的情况。
活动从onPaused()
回调返回后,系统没有义务将实例保留在内存中。 但是,如果您在某个时候调用Activity.finish()
,则该对象肯定会被破坏。
无论如何,将Activity1状态保存在“共享首选项”中的想法是可行的。 首选的替代方法是使用Activity.onSaveInstanceState() ,但这不会使您的生活更加轻松,并且也不安全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.