我有一個包含圖像拾取功能選擇圖像的活動。一旦選定,這些圖像將加載/顯示在GridView
中。我用下面的方法裝載使用ImageLoader
庫中的圖像:
ImageLoader.getInstance().displayImage("file://"+image.path, Utility.displayImageOptions);
在相同的活性我有一個預覽按鈕點擊它導致另一個新的活動。此新活動還包含使用與上一活動相同的適配器代碼的GridView
。但是,此GridView
包含先前選擇的圖像的壓縮版本。
在我的壓縮中,我保存這些位圖並創建在第二個活動中加載的新文件。
saveBitmapToFile(bitmap, index);
private void saveBitmapToFile(Bitmap bitmap, int imageIndex){
try {
bitmapFile = getPermanentFile(imageIndex);
FileOutputStream fos = new FileOutputStream(bitmapFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Images image = new Images(bitmapFile.getAbsolutePath(), true, false);
compressedImageList.add(image);
}
catch (IOException e){
e.printStackTrace();
}
}
private static File getPermanentFile(int imageIndex) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE + String.valueOf(imageIndex) + ".jpg");
try {
if (file.exists()){
Log.v("File exists", file.getName());
file.delete();
file = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE + String.valueOf(imageIndex) + ".jpg");
}
file.createNewFile();
} catch (IOException e) {
}
return file;
} else {
return null;
}
}
我的問題是,說我開始一個新的應用程序(沒有存儲在高速緩存),選擇4個圖像並單擊預覽,我得到這四個圖像的壓縮版本。但後來我關閉了應用程序,並再次啓動它,我再次選擇4個不同的圖像並點擊預覽,但我得到了相同的4壓縮圖像,我第一次得到。通過進入設置清除緩存/數據可解決問題,但我如何在代碼中執行此操作。
我刪除這些文件onBackPressed()
但仍然問題仍然存在,當我點擊回來,然後再次選擇圖像。
@Override
public void onBackPressed(){
super.onBackPressed();
for (Images file : compressedImageList){
File f = new File(file.cardPath);
boolean deleted = f.delete();
if (deleted){
Log.v("File deleted : ", file.cardPath);
}
}
compressedImageList.clear();
}
。 – Bharatesh
@bharat是我做的請看到更新的文章 – stud91
你有沒有檢查過後面按下,文件從SD卡刪除。當應用程序關閉時檢查一次sd卡片 – Bharatesh