我有一个来 self 的 rest API 的图片 url。现在我想在加载 Activity 时将其设置为 ImageView 。下面是我如何从 rest api 获取 bean,然后从中获取 URL。
Message message=new Message();
String imageUrl=message.getImageUrl();
我从我的数据库中获取 Message 对象,图像 url 包含在该 Message 对象中。
然后我使用 Url 对象获取该图像 url。
URL url = null;
try {
url = new URL(imageUrl);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
contentImageView.setImageBitmap(bmp);
} catch (Exception e) {
e.printStackTrace();
}
我使用上面的代码将图像加载到 ImageView 对象,该对象是 contentImageView
。
但我仍然无法将此图像加载到 imageview,没有加载任何内容。
有什么想法吗?
最佳答案
最简单的方法是使用 Picasso 或 Glide 之类的工具:
Picasso.with(getContext()).load(imgUrl).fit().into(contentImageView);
您可以在您的 gradle 中添加 picasso 库:
编译 'com.squareup.picasso:picasso:2.5.2'
关于安卓 : How to set an image to an imageview from a url programatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43971819/