2013-04-24 80 views
-1

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

为什么hashmapobject没有被序列化?我去哪里错了。如果我错误地缓存图像,能否请您以正确的方式进行处理?

+0

java.io.NotSerializableException .... java.io.NotSerializableException .... java.io.NotSerializableException – Selvin 2013-04-24 12:38:25

回答

0

,因为该类Drawable是不是你不能写你的HashMap Serializable

Drawable没有实现Serializable

0

它只是不会以这种方式工作。错误说,你正试图序列化一个Drawable,这是没有实现的,因为有工厂以不同的方式这样做。这个hashmap没有简单的解决方案。