我正在从 Internet 下载大量图像并存储到 ahashmap
中并显示在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
没有被序列化?我哪里出错了。如果我缓存图像有误,您能否以正确的方式告诉我?