我有一个图片网址。我想将该图片设置为我的ImageViews背景。 请帮忙..
答案 0 :(得分:0)
我希望此代码可以帮助您。
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(位图);
答案 1 :(得分:0)
检查此链接How to display image from URL on Android
然后,您可以将setBackgroundDrawable()
与从URL创建的drawable一起使用。