我想从网页上获取一些图像(格式为.gif),然后在我的应用中更改ImageView小部件。
我在stackoverflow上找到了以下代码:
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
然后,在onPostExecute方法中,我添加了以下不起作用的行(虽然它没有给出任何可见的错误)。
((ImageView) findViewById(R.id.imgWeather)).setImageDrawable(LoadImageFromWebOperations(pic_image)); //pic_image is the url which ends in .gif
答案 0 :(得分:0)
试试这个,
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
并将其称为,
new DownloadImageTask("//(ImageView) findViewById(R.id.imgWeather)")
.execute("//pic_image is the url which ends in .gif");