【发布时间】:2013-04-24 12:35:25
【问题描述】:
我正在从 Internet 下载大量图像并将其存储到 hashmap 并显示在 ListView 中。我想缓存图像。为此,我将Hashmap 对象存储到我活动的onDestroy() 内的文件中,并取回oncreate() 内的对象。
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
标签: android serialization hashmap lazy-loading drawable