Android的形象并没有从图像的URL显示给ImageView的 [英] Android image didn't display to ImageView from image URL

查看:371
本文介绍了Android的形象并没有从图像的URL显示给ImageView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从显示图像的ImageView http://i.imgur.com /4GLKF4Q.jpg,但在图像URL的图像不显示到ImageView的。我可以这样做吗?

的AsyncTask

 类DownloadImageTask扩展的AsyncTask<弦乐,无效的byte []> {
    ImageView的bmImage;    公共DownloadImageTask(ImageView的bmImage){
        this.bmImage = bmImage;
    }    受保护的byte [] doInBackground(字符串的URL ...){
        尝试{
            网址URL =新的URL(网址[0]);
            InputStream为=(InputStream的)url.getContent();
            字节[]缓冲区=新的字节[8192];
            INT读取动作;
            ByteArrayOutputStream输出=新ByteArrayOutputStream();
            而((读取动作= is.​​read(缓冲液))!= - 1){
                output.write(缓冲液,0,读取动作);
            }
            返回output.toByteArray();
        }赶上(MalformedURLException的E){
            e.printStackTrace();
            返回null;
        }赶上(IOException异常五){
            e.printStackTrace();
            返回null;
        }
    }    保护无效onPostExecute(字节[]的结果){
        ByteArrayInputStream进行的ImageStream =新ByteArrayInputStream的(结果);
        位图BM = BitmapFactory.de codeStream(的ImageStream);
        bmImage.setImageBitmap(BM);
    }
}

在活动

 新DownloadImageTask((ImageView的)newView.findViewById(R.id.thumbnail))
            .execute(http://i.imgur.com/4GLKF4Q.jpg);

在图像URL的图像不显示到ImageView的。
 在logcat中:

 十月9日至23日:26:49.410 10591-10591 / COM *** D / Skia的:--- SkImageDe codeR ::厂返回null


解决方案

从评论更新:改变图像服务器到另一个解决问题。我想这是一个 i.imgur.com 的问题。


您不应该使用自己的图像加载器的任务,它会泄漏的地方。
两个很好的图书馆存在:
- 壁画(从Facebook)
- 毕加索(从广场上)

还有UniversalImageLoader(UIL)。

I want to display image to ImageView from "http://i.imgur.com/4GLKF4Q.jpg" but The image in image URL didn't display to imageview. Can I do this?

AsyncTask

class DownloadImageTask extends AsyncTask<String, Void, byte[]> {
    ImageView bmImage;

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

    protected byte[] doInBackground(String... urls) {
        try {
            URL url = new URL(urls[0]);
            InputStream is = (InputStream) url.getContent();
            byte[] buffer = new byte[8192];
            int bytesRead;
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            while ((bytesRead = is.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
            return output.toByteArray();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    protected void onPostExecute(byte[] result) {
        ByteArrayInputStream imageStream = new ByteArrayInputStream(result);
        Bitmap bm = BitmapFactory.decodeStream(imageStream);
        bmImage.setImageBitmap(bm);
    }
}

in activity

new DownloadImageTask((ImageView) newView.findViewById(R.id.thumbnail))
            .execute("http://i.imgur.com/4GLKF4Q.jpg");

The image in image URL didn't display to imageview. in Logcat :

09-23 10:26:49.410 10591-10591/com.*** D/skia: --- SkImageDecoder::Factory returned null

解决方案

Update from comment : changing image server to another one solve the issue. I guess it's a i.imgur.com issue.


You shouldn't use your own image loader task, it will leak somewhere. Two good library exist : - Fresco (from Facebook) - Picasso (from Square up)

There is also UniversalImageLoader (UIL).

这篇关于Android的形象并没有从图像的URL显示给ImageView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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