问题
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