温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么显示网络图片

发布时间:2021-06-26 15:01:19 阅读:107 作者:Leah 栏目:移动开发
Android开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇文章给大家分享的是有关Android中怎么显示网络图片,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Android显示网络图片Step1:

1、创建你的Activity,本例中以ViewWebImageActivity说明;

2、ViewWebImageActivity中的代码如下:

String imageUrl = "http://hiphotos.baidu.com/baidu/pic/item/7d8aebfebf3f9e125c6008d8.jpg"; //这就是你需要显示的网络图片---网上随便找的  Bitmap bmImg;   ImageView imView;  Button button1;   @Override   public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.main);   imView = (ImageView) findViewById(R.id.imview);   imView.setImageBitmap(returnBitMap(imageUrl));   }   public Bitmap returnBitMap(String url) {   URL myFileUrl = null;   Bitmap bitmap = null;   try {   myFileUrl = new URL(url);   } catch (MalformedURLException e) {   e.printStackTrace();   }   try {   HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();   conn.setDoInput(true);   conn.connect();   InputStream is = conn.getInputStream();   bitmap = BitmapFactory.decodeStream(is);   is.close();   } catch (IOException e) {   e.printStackTrace();   }   return bitmap;   }

3、其中,returnBitMap(String url) 方法就是具体实现网络图片转换成bitmap。

Android显示网络图片Step2:

1、修改你的main.xml文件如下:

?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > < ImageView   android:id="@+id/imview" android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_gravity="center"   />   < /LinearLayout> 

Android显示网络图片Step3:

在你的AndroidManifest.xml文件的< /manifest>节点上面添加< uses-permission android:name="android.permission.INTERNET" />,这是由于Android有很多的权限限制,否则图片是不能在你的模拟器上显示的。

以上就是Android中怎么显示网络图片,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×