0

I'm trying to get an image from a Url into an ImageView. I've searched for it all day and I've tried lots of different solutions but none of them work for me...the image doesn't load. This is my code:

  String imageUrl = "http://www.photo-flash-maker.com/img/file_formats/jpg_file.jpg";

   iv.setImageDrawable(getImage(imageUrl));

 //...

  public static Drawable getImage(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (Exception e) {
        return null;
    }
}

I found it here: How to display image from URL on Android because this seems to be the simplest solution but the image doesn't load at all. Any solutions?

4

5 回答 5

1

您可以使用BitmapFactory

Bitmap bitmap;
try {
    bitmap = BitmapFactory.decodeStream(new URL(imageUrl).openConnection().getInputStream());
    Log.i("image", "retrieved");
} catch (Exception ex) {
    ex.printStackTrace();
}
于 2012-08-21T13:23:03.650 回答
1

您是否在 manifest.xml 文件中设置了允许您的应用程序从 Internet 加载图像的权限?

<manifest xlmns:android...>  
    ...  
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest> 

也试试这个链接:http ://android-er.blogspot.co.uk/2010/06/load-imageview-with-bitmap-from.html

包含您的 imageview 所需的所有 xml 更改

于 2012-08-21T13:23:48.393 回答
0

我认为最好的解决方案是评论中建议的一个 zapi:

不要在 UI 线程上下载。您的应用将 ANR。使用/编写类似 github.com/nostra13/Android-Universal-Image-Loader

但是,AA 和 RossC 的其他解决方案也可能有效。

于 2012-08-21T13:51:02.503 回答
0

我想你会在这里得到你所需要的

于 2012-08-21T13:28:19.347 回答
0

您是否尝试添加权限写入外部存储 android:name="android.permission.WRITE_EXTERNAL_STORAGE"

试试这两个我不确定它是否有帮助

于 2012-08-21T13:54:29.440 回答