调用我的网址时,会自动从服务器下载图片。我需要在Android ImageView上显示此图像。我通常使用Piccaso库从网址加载图片,但这对我没有帮助,有没有办法将图片从自动下载网址加载到Android ImageView?
自动下载网址示例位于here(wallpaperswide.com/download/bike_chase-wallpaper-2560x1600.jpg)
答案 0 :(得分:0)
好吧如果不想像Piccaso那样使用像这样的东西:
public class AsyncTaskLoadImage extends AsyncTask<String, String, Bitmap> {
private final static String TAG = "AsyncLoadImage";
private ImageView imageView;
public AsyncTaskLoadImage(ImageView imageView) {
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
try {
URL url = new URL(params[0]);
bitmap = BitmapFactory.decodeStream((InputStream)url.getContent());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
imageView.setImageBitmap(bitmap);
}
}
你打电话给你的活动,如:
String url = "YOUR_LINK_HERE";
new AsyncTaskLoadImage(imageView).execute(url);
或强>
使用较少的代码尝试like this answer:
URL url = new URL("YOUR_LINK_HERE");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);