main.xml:

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

MyWebDemo.java:

import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.widget.ImageView;public class MyWebDemo extends Activity {private static final String PATH = "http://image2.sina.com.cn/pfp/ask/images/zhishi/logo_zhishi.gif"; private ImageView img = null ;// 定义图片显示@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.main);// 调用布局管理器this.img = (ImageView) super.findViewById(R.id.img) ;// 取得组件try {byte data [] = this.getUrlData() ;// 接收数据Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);// 生成图形 this.img.setImageBitmap(bm) ;// 显示图片} catch (Exception e) {e.printStackTrace();}}public byte[] getUrlData() throws Exception {// 取得网络图片数据ByteArrayOutputStream bos = null ;// 内存输出流try {URL url = new URL(PATH) ;// 定义URLbos = new ByteArrayOutputStream() ;// 定义内存输出流byte data [] = new byte[1024] ;// 每次读取1024HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 打开连接InputStream input = conn.getInputStream() ;// 取得输入流int len = 0 ;// 接收读取长度while((len = input.read(data)) != -1) {// 没有读取到底部bos.write(data, 0, len) ;// 向内存中保存}return bos.toByteArray() ;// 变为字节数组返回} catch (Exception e) {throw e ;} finally { if (bos != null) {bos.close();// 关闭输出流}}}}

更多相关文章

  1. android桌面图标读取和存储
  2. 自定义数据适配器
  3. Android(安卓)读取联系人(详细)
  4. Unable to instantiate receiver
  5. Android(安卓)测试自定义纯数字软键盘
  6. pagertab 自定义控件碎片滑动
  7. Android文件简单读写操作
  8. [Android][自定义进度条]④--圆形进度条的onMeasure
  9. 自定义控件之组合控件

随机推荐

  1. Android根据Button状态(normal,focused,p
  2. Android按键响应之初见
  3. android入门:zxing学习笔记
  4. 企业开发与Android
  5. Android中SQLite应用详解(sql访问数据库)
  6. Android跨进程通信-IPC初探(三) - 使用AI
  7. Android应用于军事制造业,开放性优势受青
  8. 关于Android软件盘的显示与隐藏的问题
  9. 新建android project和其配置文件的基本
  10. 《IT蓝豹》android 7种网络连接方式