在android系统加载位图的ImageView从网址 [英] Loading Bitmap to ImageView from URL in android

查看:134
本文介绍了在android系统加载位图的ImageView从网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的方法来从URL检索位图,并把它传递到ImageView的,但ImageView的不被更新。

 公共静态位图LoadImageFromWebOperations(字符串URL){
    位图位图;
    尝试{
        InputStream的是=新的URL(网址).openStream();
       位= BitmapFactory.de codeStream(是);
        返回位图;
    }赶上(例外五){
         返回null;
    }

通话 -

  mov1_poster.setImageBitmap(VPmovies.LoadImageFromWebOperations(mov_details [0] [7]));
//不起作用  Toast.makeText(这一点,网址为\\ n+ mov_details [0] [7],Toast.LENGTH_LONG).show();
   //显示图像的URL成功(只是​​查询的网址不为null)

有什么我做错了什么?请帮助。


解决方案

 公共类DownloadImageTask扩展的AsyncTask<弦乐,太虚,位图> {
    私人ImageView的ImageView的;
    私人位图图像;    公共DownloadImageTask(ImageView的ImageView的){
        this.imageView = ImageView的;
    }    保护位图doInBackground(字符串的URL ...){
        字符串urldisplay =网址[0];
        尝试{
            在的InputStream =新的java.net.URL(urldisplay).openStream();
            图像= BitmapFactory.de codeStream(中);
        }赶上(例外五){
            图像= NULL;
        }
        返回形象;
    }    @燮pressLint(NewApi)
    保护无效onPostExecute(位图结果){
        如果(结果!= NULL){
            imageView.setImageBitmap(结果);
        }
    }
}

现在,在您code拨打:

 新DownloadImageTask(YOUR_IMAGE_VIEW).execute(YOUR_URL);

I am using following method to retrieve a bitmap from the url and pass it on to the imageview , but the imageview is not being updated.

 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;
    }

call -

  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)

Is there anything i am doing wrong ? Please help.

解决方案

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);
        }
    }
}

Now call in your code:

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

这篇关于在android系统加载位图的ImageView从网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆