在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。

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>

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

如此一来,你的目的就达到了,简单吧,那还不赶紧尝试!

更多相关文章

  1. android google map v2的小例子 美洲地图的显示
  2. Android中EditText属性
  3. Android中通过Intent 调用图片、视频、音频、录音、拍照
  4. android GridView选择照片的完整实现
  5. Android(安卓)P 图形显示系统(四) Android(安卓)VirtualDisplay解
  6. Android图形库Skia(一)-基本测试生成PNG图片
  7. Android(安卓)Map库
  8. Android(安卓)调用相册 拍照 实现系统控件缩放 切割图片
  9. Android(安卓)中 PopupWindow的用法 汇总

随机推荐

  1. 地图入门(一):Android上使用Google Maps加标
  2. android图形系统详解二:Drawables
  3. Android中的DownloadManager
  4. Android之使用传感器获取相应数据
  5. android定时任务实现
  6. android定义圆形按钮Shape,选中改变颜色
  7. Android 图像处理(类型转换,比例缩放,倒影,圆
  8. "adb"不是内部命令和sudo: adb: command
  9. dex替换方式实现热修复
  10. 【Android病毒分析报告】 - Obad