Save array of drawable to file Android

一个人想着一个人 提交于 2019-12-11 04:33:52

问题


im trying to save an arraylist of drawable to a file.

Here is my class.

public class SeccionItem implements Serializable{
    String name;
    String text;
    ArrayList<Drawable> img;

    SeccionItem()
    {
        img = new ArrayList<Drawable>();
    }
}

I have an arraylist of that class and i want to write it to a file using objectoutputstream, but i think that drawable cant be serialized, so can i use another type to store the images? like bitmap?. or is there any other way to store that arraylist? overwriting writeObject method?.

Im using this method to download the images

  public static Drawable getBitmap(String image_url) {
      try {
        URL url = new URL(image_url);
        InputStream is = (InputStream)url.getContent();
        Drawable b= Drawable.createFromStream(is, " ");
        if(b==null){
            Log.d("this","null");
        }
        return b;
      }catch(Exception ex) {
          ex.printStackTrace();
          return null;
      }
  }

回答1:


neither Bitmap or Drawable are serializable. You could serialize the information to rebuild your Drawable. For instance you could serialize an ArrayList<Integer> where the Intever is the Drawable's id.

that drawables are downloaded from internet, i want to store it so next time i dont have to download it again.

So you can store it on the sdcard, and nex time you can check if the file exists or not.

To write a file

public static void copy(InputStream is, File out) throws IOException {
                byte[] buffer = new byte[BUFFER_LEN];
                FileOutputStream fos = new FileOutputStream(out);
                try {
                        int read = 0;
                        while ((read = is.read(buffer, 0, BUFFER_LEN)) >= 0) {
                                fos.write(buffer, 0, read);
                        }
                        fos.flush();
                } finally {
                        close(fos);
                }

                fos = null;
                buffer = null;
}


来源:https://stackoverflow.com/questions/17042130/save-array-of-drawable-to-file-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!