嗨,我有一个棘手的问题,看似简单,但是自从我开始研究以来,一直在逗弄我的大脑。
我的应用程序允许用户输入产品编号和图像编号。该应用可以为一种产品拍摄多张图片。
将图像保存到图库时,我想为其添加一个顺序的文件号,以便知道哪些图像链接到了哪个产品。
以下是文件保存的格式:
String fname = TypeNo + "(" + ImageNo + ")" + sequentialNumber + ".jpg";
例如,
Type No = Test1;
ImageNo = 1;
sequentialNumber = 1;
fname = Test1(1)1;
当用户保存此编号时,他们将返回到数据输入活动,如果保持相同的TypeNo / ProductCode,则下次保存时,sequenceNo应该保持不变。
如果用户输入其他TypeNo / ProductNo,则序列号将递增。
这是我到目前为止尝试过的代码,当输入新的TypeNo时,该代码无法正确递增:
private void saveImageToExternalStorage(Bitmap bitmap_view) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/Digital_Images");
myDir.mkdirs();
String fname = TypeNo + "(" + ImageNo + ")" + sequentialNumber + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap_view.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences.Editor editor = sharedPreferences.edit();
String lastUsedTypeNo = TypeNo;
editor.putString("TypeNo", lastUsedTypeNo);
editor.putInt("sequentialNum", sequentialNumber);
editor.apply();
}
@Override
public void onResume() {
super.onResume();
String TypeNoRetrieved = sharedPreferences.getString("TypeNo", null);
int SQNumRetrieved = sharedPreferences.getInt("sequentialNum", 1);
if (TypeNoRetrieved != null || SQNumRetrieved != 0) {
if (TypeNoRetrieved == lastUsedTypeNo ) {
sequentialNumber = SQNumRetrieved;
} else {
sequentialNumber++;
}
}
}
有人能为我解决这个难题吗?