我已經看到了這個問題:android how to download an 1mb image file and set to ImageView
它並沒有解決我的問題,因爲它只是說明如何顯示位圖後你已經擁有它。下載圖像在Android
我正嘗試從URL下載圖像,使其與Android設備上的ImageView一起顯示。我不知道如何做到這一點。
我已經在互聯網上有點看,這是我到目前爲止的代碼:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set local image
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test2);
//Prepare to download image
URL url;
InputStream in;
//BufferedInputStream buf;
try {
url = new URL("http://i.imgur.com/CQzlM.jpg");
in = url.openStream();
out = new BufferedOutputStream(new FileOutputStream("testImage.jpg"));
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
out.close();
in.close();
buf = new BufferedInputStream(in);
Bitmap bMap = BitmapFactory.decodeStream(buf);
image.setImageBitmap(bMap);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
}
如果你找到答案,你應該接受它。這樣,其他用戶知道它的工作原理。祝你好運! – Entreco