1. 程式人生 > >圖片url轉成Drawable

圖片url轉成Drawable

圖片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();