您好我看過類似的問題,但我無法解決問題。它不工作。 我想從服務器下載一些圖像,並將它們放在android應用程序中的ImageViews中。Android - 從服務器下載圖像並加載到ImageView中
public class MyAsyncTask extends AsyncTask {
protected String doInBackground(String... params) {
...........
URL urlImagine = new URL("http://s.ytimg.com/yts/img/favicon-vfldLzJxy.ico");
//Version 1:
URLConnection conn = urlImagine.openConnection();
inputStreamImagine = (InputStream)conn.getContent();
..........
//version 2:
inputStreamImagine = urlImagine.openStream();
bufImagine = new BufferedInputStream(inputStreamImagine);
.........
}
}
//Some other class:
..............
//http://stackoverflow.com/questions/15569953/download-image-from-image-url-to-image-view
//version 1:
//Download Image From Image URL to image view
if (inputStreamImagine!=null){
Bitmap bitmap = BitmapFactory.decodeStream(inputStreamImagine);
imageView.setImageBitmap(bitmap);
}
//version 2:
if (bufImagine!=null){
// Convert the BufferedInputStream to a Bitmap
Bitmap bMap = BitmapFactory.decodeStream(bufImagine);
Drawable drawable = new BitmapDrawable(bMap);
imageView.setBackgroundDrawable(drawable);
//iv.setImageDrawable(drawable);
}
我試過使用這兩個示例,但ImageView不加載任何圖像。 你能幫我嗎?
試試這個例子:http://androiddevelopmentworld.blogspot.in/2013/04/how-to-display- image-from-url-in-android.html –
@Prince。謝謝。這個例子很清楚,我正在追蹤它,但我不明白爲什麼它不適合我。 – Alex