首页 > 解决方案 > 如何从远程源 (url) 获取图像并在 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);

有没有办法做到这一点?

标签: javaandroidimageview

解决方案


使用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);

推荐阅读