如何从远程源 (url) 获取图像并将其显示在 ImageView 中?

How do I get an image from a remote source (url) and show it in ImageView?

我正在构建一个 Android Things "kiosk" 风格的应用程序,我希望图像像墙纸一样显示在背景中。我正在使用 Unsplash Source 来获取随机图像,但是源 URL (https://source.unsplash.com/1080x1920/?long-exposure) 总是重定向到图像 URL,所以我不能使用这个:

InputStream is = (InputStream) new URL("https://source.unsplash.com/1080x1920/?long-exposure").getContent();
Drawable d = Drawable.createFromStream(is, "");
niceWallpaper.setImageDrawable(d);

有办法吗?

使用Picasso.

Picasso.get().load("https://source.unsplash.com/1080x1920/?long-exposure")
                            .into(imageView);

使用Picasso可以解决您的问题。

String yourUrl = "https://source.unsplash.com/1080x1920/?long-exposure";
Picasso.with(MyApplication.getAppContext()).load(yourUrl).placeholder(defaultImage).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).into(YourImageView);