Android显示网络图片实例
Android显示网络图片实例
发布时间:2016-12-28 来源:查字典编辑
摘要:本文实例讲述了Android显示网络图片的方法,分享给大家供大家参考。具体方法如下:一般来说,在Android中显示一张网络图片其实是非常简...

本文实例讲述了Android显示网络图片的方法,分享给大家供大家参考。具体方法如下:

一般来说,在Android中显示一张网络图片其实是非常简单的,下面就是一个非常简单的例子:

步骤1:

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

② ViewWebImageActivity中的代码如下:

复制代码 代码如下:String imageUrl = "http://www.jb51.net/images/logo.gif"; //这就是你需要显示的网络图片---网上随便找的

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;

}

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

步骤2:

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

复制代码 代码如下:<"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>

步骤3:

在你的AndroidManifest.xml文件的节点上面添加,这是由于Android有很多的权限限制,否则图片是不能在你的模拟器上显示的。

希望本文所述对大家的Android程序设计有所帮助。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新安卓软件开发学习
热门安卓软件开发学习
编程开发子分类