我正在從互聯網下載大量圖像並將其存儲到hashmap
中,並在ListView
中顯示。我想緩存圖像。爲此,我將Hashmap
對象存儲到我的活動的onDestroy()
內的文件中,並取回oncreate()
中的對象。無法將哈希表對象保存到Android中的文件
Map<String, Drawable> hashmap; //storing the urls of images and downloaded image `Drawables` into this map
內onCreate()
:
File f = new File(Environment.getExternalStorageDirectory()+"/image_cache.ser");
if(f.exists()){
FileInputStream fin = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fin);
hashmap = (HashMap<String, Drawable>)ois.readObject();
ois.close();
Here creating a custom adapter and showing the hashmap drawable images in a listview
}
裏面的onDestroy()
@Override
protected void onDestroy() {
super.onDestroy();
File f = new File(Environment.getExternalStorageDirectory()+"/image_cache.ser");
try {
if(hashmap != null && hashmap.size() >= 1){
FileOutputStream fout = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(hashmap);
oos.flush();
oos.close();
}
} catch (Exception e) {
e.printStackTrace();
f.delete();
}
}
問題:獲取此異常:
java.io.NotSerializableException android.graphics.drawable.BitmapDrawable inside ondestoy().
爲什麼hashmap
object
沒有被序列化?我去哪裏錯了。如果我錯誤地緩存圖像,能否請您以正確的方式進行處理?
java.io.NotSerializableException .... java.io.NotSerializableException .... java.io.NotSerializableException – Selvin 2013-04-24 12:38:25