私はアンドロイドの初心者です。アンドロイドのカメラで問題に直面しました。
私のクラスでは、いくつかのインスタンス変数 (いくつかの値を保持) と、ボタンとイメージ ビューがあります。
そのボタンをクリックすると、アプリケーションがカメラを開き、SD カードに保存します。その後、インスタンス変数を使用していくつかの操作を行っています。
しかし問題は、変数がデフォルト値に再初期化されることです。
別のクラスで共有設定または保存 (変数データ) を静的変数として使用した場合、正常に動作します。
私の質問は、すべてのインスタンス変数が再初期化される理由です。
そして、その問題をどのように克服するのですか?
静的変数として別のクラスに(変数データ)を格納するSharedPreferenceは、唯一の解決策ですか?
カメラコード
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 us.\n"
+ e.toString());
}
} else {
Helper.AlertBox(this, "Camera Not Found.!");
}
}
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);
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 Bluefrog technical person.\n"
+ e.toString());
Log.e("Error reading file", e.toString());
}
} else {
Helper.AlertBox(this,
"Error No: 005\nPlease contact Bluefrog technical person.");
}
} else {
Toast.makeText(getApplicationContext(), "Cam Not Supported",
5000).show();
}
}
}
here Helper.index(静的変数としていくつかのクラスに保存しました)
前もって感謝します。