加载远程图像 [英] Loading remote images

查看:166
本文介绍了加载远程图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,什么是最简单的方法如下:

  1. 从远程服务器加载图像。
  2. 在显示它的ImageView的。
解决方案

请参阅<一href="http://developer.android.com/reference/android/widget/ImageView.html#setImageURI(android.net.Uri)">ImageView.setImageURI.是这样的:

  myImageView.setImageURI(Uri.parse(http://example.com/logo.png));
 

和你就大功告成了。


编辑:我没有测试过上面的方法,并从评论,似乎这是行不通的。这里是另一种方法,我在应用程序中实际使用的,我知道它的工作原理:

 尝试{
    URL thumb_u =新的URL(http://www.example.com/image.jpg);
    抽拉thumb_d = Drawable.createFromStream(thumb_u.openStream(),SRC);
    myImageView.setImageDrawable(thumb_d);
}
赶上(例外五){
    // 处理它
}
 

我不知道的第二个参数 Drawable.createFromStream 是的,但通过SRC似乎工作。如果有谁知道,请闪耀光芒,作为文档真的不说什么。

In Android, what is the simplest approach to the following:

  1. Load an image from a remote server.
  2. Display it in an ImageView.

解决方案

See ImageView.setImageURI. Something like:

myImageView.setImageURI(Uri.parse("http://example.com/logo.png"));

And you're done.


Edit: I have not tested the above method and, from the comments, it seems it doesn't work. Here's another method that I actually used in an application and I know it works:

try {
    URL thumb_u = new URL("http://www.example.com/image.jpg");
    Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
    myImageView.setImageDrawable(thumb_d);
}
catch (Exception e) {
    // handle it
}

I have no idea what the second parameter to Drawable.createFromStream is, but passing "src" seems to work. If anyone knows, please shed some light, as the docs don't really say anything about it.

这篇关于加载远程图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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