下面添加權限下載圖像的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;
比你可以通過呼叫建立形象,方法
image.setImageBitmap(bitmap);
希望它會幫助你
您只能在代碼(Java)中執行此操作。或者,首先下載圖像,然後將其放入可繪製文件夾中,然後**從佈局(xml)中將其引用。 –
@ FrankN.Stein謝謝,我使用了下面建議的Picassa庫。 – Stas