Android 加载远程图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3075637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:35:52  来源:igfitidea点击:

Loading remote images

androidimageimageview

提问by Emanuil Rusev

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

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

  1. Load an image from a remote server.
  2. Display it in an ImageView.
  1. 从远程服务器加载图像。
  2. 在 ImageView 中显示它。

回答by Felix

Here's a 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.createFromStreamis, but passing "src"seems to work. If anyone knows, please shed some light, as the docs don't really say anything about it.

我不知道第二个参数Drawable.createFromStream是什么,但传递"src"似乎有效。如果有人知道,请说明一下,因为文档并没有真正说明它。

回答by Cristian

The easiest way so far is build a simple image retriver:

到目前为止,最简单的方法是构建一个简单的图像检索器:

public Bitmap getRemoteImage(final URL aURL) {
    try {
        final URLConnection conn = aURL.openConnection();
        conn.connect();
        final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        final Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        return bm;
    } catch (IOException e) {}
    return null;
}

Then, you just have to supply a URL to the method and it will returns a Bitmap. Then, you will just have to use the setImageBitmapmethod from ImageViewto show the image.

然后,您只需为该方法提供一个 URL,它就会返回一个Bitmap. 然后,您只需要使用setImageBitmapfrom 方法ImageView来显示图像。

回答by Hamy

Be careful with both of the answers here - they both run the chance of an OutOfMemoryException. Test your application by attempting to download a large image, such as a desktop wallpaper. To be clear, the offending lines are :

小心这里的两个答案 - 它们都有可能出现OutOfMemoryException. 通过尝试下载大图像(例如桌面墙纸)来测试您的应用程序。需要明确的是,违规行是:

final Bitmap bm = BitmapFactory.decodeStream(bis);

final Bitmap bm = BitmapFactory.decodeStream(bis);

and

Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");

Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");

Felix's answer will catch it in the catch{} statement, and you could do something about it there.

Felix 的回答会在 catch{} 语句中捕获它,你可以在那里做一些事情。

Here is how to work around the OutOfMemoryExceptionerror:

以下是解决该OutOfMemoryException错误的方法:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    Bitmap bmp = null;
    try {
        bmp = BitmapFactory.decodeStream(is, null, options);
    } catch (OutOfMemoryError ome) {
        // TODO - return default image or put this in a loop,
        // and continue increasing the inSampleSize until we don't
        // run out of memory
    }

And here are my comments on this in my code

这是我在代码中对此的评论

/**
 * Showing a full-resolution preview is a fast-track to an
 * OutOfMemoryException. Therefore, we downsample the preview image. Android
 * docs recommend using a power of 2 to downsample
 * 
 * @see <a
 *      href="https://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">StackOverflow
 *      post discussing OutOfMemoryException</a>
 * @see <a
 *      href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize">Android
 *      docs explaining BitmapFactory.Options#inSampleSize</a>
 * 
 */

Links from above comments: Link 1Link 2

来自上述评论的链接链接 1链接 2

回答by Daniel Dudek

You can also try this lib: https://github.com/codingfingers/fastimage

你也可以试试这个库:https: //github.com/codingfingers/fastimage

When we had few projects with the same pattern, and the lib came up ;) so why not to share with others...

当我们几乎没有使用相同模式的项目时,lib 出现了 ;) 那么为什么不与其他人分享......

回答by Kristian

This is simple:

这很简单:

Add this dependency in you gradle script:

在您的 gradle 脚本中添加此依赖项:

implementation 'com.squareup.picasso:picasso:2.71828'

*2.71828 is current version

*2.71828 是当前版本

Then do this for the image view:

然后对图像视图执行此操作:

Picasso.get().load(pictureURL).into(imageView);