绘制保存到文件的Android的数组 [英] Save array of drawable to file Android
问题描述
我尝试绘制的ArrayList保存到一个文件中。
下面是我的课。
公共类SeccionItem实现Serializable {
字符串名称;
字符串文本;
ArrayList的<&可绘制GT; IMG; SeccionItem()
{
IMG =新的ArrayList<&可绘制GT;();
}
}
我有这个类的一个ArrayList,我想将它写入使用ObjectOutputStream一个文件,但我认为,绘制着被序列化,所以我可以使用另一种类型的存储图像?像位图?或者是有任何其他方式来存储数组列表?重写writeObject方法?
即时通讯使用这种方法下载图像
公共静态可绘制getBitmap(字符串IMAGE_URL){
尝试{
网址URL =新的URL(图片网址);
InputStream为=(InputStream的)url.getContent();
可绘制B = Drawable.createFromStream(是,);
如果(二== NULL){
Log.d(这个,空);
}
返回b;
}赶上(例外前){
ex.printStackTrace();
返回null;
}
}
既不位图
或可绘制
是序列化
。你可以序列化信息来重建可绘制
。例如,你可以序列化的ArrayList<整数GT;
,其中Intever是绘制对象的ID。
这是可绘制从互联网上下载的,我想将其存放
下一次我不必重新下载。
块引用>所以,你可以把它存储在SD卡,和NEX时间您可以检查文件是否存在与否。
要写入文件
公共静态无效的复印件(InputStream为,文件输出)抛出IOException
字节[]缓冲区=新的字节[BUFFER_LEN]
FOS的FileOutputStream =新的FileOutputStream(出);
尝试{
INT读= 0;
而((读取= is.read(缓冲液,0,BUFFER_LEN))GT; = 0){
fos.write(缓冲,0,读);
}
fos.flush();
} {最后
关闭(FOS);
} FOS = NULL;
缓冲= NULL;
}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; } }
解决方案neither
Bitmap
orDrawable
areserializable
. You could serialize the information to rebuild yourDrawable
. For instance you could serialize anArrayList<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; }
这篇关于绘制保存到文件的Android的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!