我正在从互联网下载大量图像并将其存储到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