【Android 开发】:UI控件之 ImageView 从网络上获取图像
来源:互联网 发布:天族女捏脸数据图 编辑:程序博客网 时间:2024/05/17 02:14
在我们的手机应用开发中,经常要进行客户端与服务端之间的交互,我们可能会从网络上获取一张图像显示在我们的手机上,这样我们可以使用HTTP协议进行操作。这一讲我们将学习如何从服务端获取图片到手机客户端
案例:点击按钮从网络上获取ImageView图像显示在本地
程序实现:
案例:点击按钮从网络上获取ImageView图像显示在本地
程序实现:
1. 布局文件<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下载网络图片"/> </LinearLayout>
2. 程序主要代码1)HttpUtils.java 作为辅助类[注意编程规范]: 如果方法A里面含有了 try catch模块来捕获异常,在方法B中调用A的过程中也需要try catch 一次异常。则这种情况下一般要把A的异常抛出,让方法B去捕获这个异常。public class HttpUtils { private final static String URL_PATH = "http://www.android.com/images/about/about-nexus-family.png"; //访问网络图片的路径 public HttpUtils() { // TODO Auto-generated constructor stub } /** * 从网络中获取图片信息,以流的形式返回 * @return * @throws IOException */ public static InputStream getImageViewInputStream() throws IOException{ InputStream inputStream = null; URL url = new URL(URL_PATH); if(url != null){ HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setConnectTimeout(3000); //设置连接超时的时间 httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoInput(true); //设置响应的代码 int response_code = httpURLConnection.getResponseCode(); if(response_code == 200){ inputStream = httpURLConnection.getInputStream(); } } return inputStream; } public static byte[] getImageViewArray(){ byte[] data = null; InputStream inputStream = null; //不需要关闭输出流,直接 写入到内存中 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { URL url = new URL("URL_PATH"); if(url != null){ HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setConnectTimeout(3000); //设置连接超时的时间 httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoInput(true); //设置响应的代码 int response_code = httpURLConnection.getResponseCode(); int len = 0; byte[] b_data = new byte[1024]; if(response_code == 200){ inputStream = httpURLConnection.getInputStream(); while((len = inputStream.read(b_data)) != -1) { outputStream.write(b_data, 0, len); } data = outputStream.toByteArray(); } } } catch (Exception e) { // TODO: handle exception }finally{ if(inputStream != null){ try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return data; }}
2) 主程序public class ImageViewDemo extends Activity { private Button button; private ImageView imageView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initComponent(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new Thread(){ public void run() { try { InputStream inputStream = HttpUtils.getImageViewInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); imageView.setImageBitmap(bitmap); //需要一个Bitmap对象,可以从 BitmapFactory工厂中获得 } catch (IOException e) { // TODO: handle exception } }; }.start(); } }); } private void initComponent(){ button = (Button)findViewById(R.id.button); imageView = (ImageView)findViewById(R.id.imageView); }}访问网络的主程序最好另起一个线程而不是写在UI主线程中。
- 【Android 开发】:UI控件之 ImageView 从网络上获取图像
- UI控件之显示图像控件ImageView(上)
- UI控件之显示图像控件ImageView(下)
- Android控件开发之ImageView
- Android控件开发之ImageView
- Android02--Android之UI控件ImageView+ImageButton
- 【Android 开发】:UI控件之显示图片控件 ImageView 的使用方法
- 【Android 开发】:UI控件之 ImageView 实现适屏和裁剪图片的功能
- 【Android 开发】:UI控件之 ImageView 实现图片旋转和缩放功能
- Android UI之ImageView
- Android--UI之ImageView
- 【android基础】之Android获取网络上的图片结合ImageView的简单应用
- [Android新手学习笔记13]-UI控件之ImageView
- android从网络上异步加载图像
- android从网络上异步加载图像
- android从网络上异步加载图像
- android URL获取图片显示到ImageView 控件上
- android 获取网络图片并在Imageview上显示
- Android开发(10)使用SQLiteDatabase操作SQLite数据库实现增删改查(源码)
- 《学习OpenCV》练习题第三章第二题
- C:表达式、语句、声明
- C++读书笔记之重载双目运算符 Cplusplus overload binary operator
- 新手教程:建立网站的全套流程与详细解释
- 【Android 开发】:UI控件之 ImageView 从网络上获取图像
- Event Validation Errors and Network Congestion in ASP.NET
- 《学习OpenCV》练习题第二章第五题
- MySQL 5.5 权限导出脚本
- AndroidQuery 开源项目
- 来自各移动平台的设计指导方针
- 一个关于兑换零钱的豆瓣笔试题
- 题目1173:查找
- 事例学习开发WEBSERVER服务器(一)