Android手机操作系统已经出现就受到了用户的好评。各大手机厂商也开始利用这一开源的系统抓住商机,发展自己的产业。在这里先来了解一下这一操作系统的一个小技巧,有关Android显示网络图片的实现。

    在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有很多的权限限制,否则图片是不能在你的模拟器上显示的

更多相关文章

  1. Android高性能编码实战:网络框架优化
  2. 使用Android内部的DownloadProvider下载文件,并获取cache权限
  3. 创建Android库的方法及Android .aar文件用法小结
  4. androidSDK下的图片资源
  5. android的init.rc文件的语法
  6. 自定义RadioButton 文字在下,图片在上
  7. android socket 配置文件
  8. Android学习笔记之mainfest文件中android属性
  9. 自定义progressbar使用图片

随机推荐

  1. Android基础(十) Service 服务
  2. Android(安卓)Debug keystore系统位置
  3. 【Android(安卓)Developers Training】 4
  4. Android:使用Socket网络通信时异常:java.ne
  5. 文章【Android(安卓)的视频编码 H263 MP4
  6. Android(安卓)程式开发:(廿一)消息传递 ——
  7. android讲义2之简单图片浏览器且有截图效
  8. Android(安卓)UI Surface 界面绘制原理
  9. Android下用gdb远程调试
  10. 二、获取wifi列表并连接wifi