图片url转成Byte[],Byte[]转成Drawable
之所以要转成Byte[],是为了能方便的保存在本地
InputStream is = (InputStream) new URL(source).getContent();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int len=-1;
byte[] buffer=new byte[1024];
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0,len);
}
imgBuffer = outStream.toByteArray();
outStream.close();
is.close();
is=new ByteArrayInputStream(imgBuffer);
Drawable d = Drawable.createFromStream(is, "src");
is.close();
直接由url得到Drawable
InputStream is = (InputStream) new URL(source).getContent();
Drawable d = Drawable.createFromStream(is, "src");
is.close();
本文详细介绍了如何将图片URL转换为Byte数组,然后将Byte数组转换为Drawable对象,以便在本地进行存储与使用。通过使用InputStream、ByteArrayOutputStream、ByteArrayInputStream等流操作,实现了URL到Drawable的高效转换过程。
2359

被折叠的 条评论
为什么被折叠?



