正在 android 中的 URL 将位图加载到 ImageView

Loading Bitmap to ImageView from URL in android

我正在使用以下方法从 url 检索位图并将其传递给图像视图,但图像视图未更新。

 public static Bitmap LoadImageFromWebOperations(String url) {
    Bitmap bitmap;
    try {
        InputStream is = new URL(url).openStream();
       bitmap = BitmapFactory.decodeStream(is);
        return bitmap;
    } catch (Exception e) {
         return null;
    }

呼叫 -

  mov1_poster.setImageBitmap(VPmovies.LoadImageFromWebOperations(mov_details[0][7]));
//doesn't work

  Toast.makeText(this,"url is \n"+mov_details[0][7],Toast.LENGTH_LONG).show();
   // shows the url of the image successfully (just to check the url is not null)

我做错了什么吗?请帮忙。

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    private ImageView imageView;
    private Bitmap image;

    public DownloadImageTask(ImageView imageView) {
        this.imageView = imageView;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            image = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            image = null;
        }
        return image;
    }

    @SuppressLint("NewApi")
    protected void onPostExecute(Bitmap result) {
        if (result != null) {
            imageView.setImageBitmap(result);
        }
    }
}

现在调用你的代码:

 new DownloadImageTask(YOUR_IMAGE_VIEW).execute("YOUR_URL");

在处理图像时使用 the picasso library,它的效果非常好,而且非常简单!

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);