可能重複:
how to load an imageview by url in android?如何從圖片網址設置imageView的背景?
我有URL.I要設置圖像作爲我ImageViews背景的圖像。 請幫忙..
可能重複:
how to load an imageview by url in android?如何從圖片網址設置imageView的背景?
我有URL.I要設置圖像作爲我ImageViews背景的圖像。 請幫忙..
檢查此鏈接How to display image from URL on Android
然後,您可以使用setBackgroundDrawable()
從URL創建的繪製。
我希望這段代碼能幫到你。
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
然後使用imageView.setImageBitmap(位圖);