热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android:如何以编程方式从URL设置图像到imageview

如何解决《Android:如何以编程方式从URL设置图像到imageview》经验,为你挑选了1个好方法。

我有一个来自我的其余API的图片网址.现在我想在加载活动时将其设置为imageview.下面是我如何从其余的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,没有任何东西被加载.

有什么想法吗?



1> Mohammad Zar..:

最简单的方法是使用像Picasso或Glide这样的东西:

Picasso.with(getContext()).load(imgUrl).fit().into(contentImageView);

你可以在你的gradle中添加picasso库: compile 'com.squareup.picasso:picasso:2.5.2'


推荐阅读
author-avatar
光陆光陆光陆
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有