【Android基础】网络图片查看器
2016-04-10 20:08
323 查看
网络图片查看器流程
从网络上获取数据:1. 发送请求:GET
2. 接收服务器端返回的响应数据
使用代码实现的步骤:
1. 创建URL,打开一个HTTP的连接;
2. 设置请求头信息:GET(GET、POST)
3. 接收服务器端返回的响应数据,响应码:200 ok,404没有找到资源 ,503服务器端内部错误
4. 把接收的二进制数据转换成图片
模版代码
//1.创建URL,打开一个HTTP的连接 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //2.设置请求头信息:GET(GET、POST) conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); //3.接收服务器端返回的响应数据,响应码:200 ok,404没有找到资源 ,503服务器端内部错误 int code = conn.getResponseCode(); if(code == 200){ InputStream is = conn.getInputStream();
访问网络的Android应用都必须加上访问互联网的权限:
<uses-permission android:name="android.permission.INTERNET"/>
网络在主线程上的异常: android.os.NetworkOnMainThreadException 从Androi4.0开始,google更加UI界面运行的流畅性,强制要求访问网络的操作不能在主线程中进行,只能在子线程中进行。
activity中的oncreate方法和单击事件的方法都是运行在主线程中的。
只有创建UI界面的那个线程才能修改UI: Only the original thread that created a view hierarchy can touch its views.
主线程(UI线程),只有主线程才能修改UI。如果子线程修改UI,系统验证当前线程是不是主线程,如果不是主线程,就会终止运行。
消息处理
//1.在主线程中创建handler private Handler handler = new Handler(){ //接收消息并处理消息 @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); } }; //2.在子线程中得到handler的引用,并发送消息给主线程 Message msg = new Message(); msg.obj = bm; handler.sendMessage(msg); //3.在主线程中修改UI public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); Bitmap bm = (Bitmap) msg.obj; //把位图设置到ImageView控件 iv.setImageBitmap(bm); }
完整的代码和效果图
界面设计的代码:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.yzx.picview.MainActivity" android:orientation="vertical"> <EditText android:id="@+id/et_path" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入图片的网络路径"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="查看"/> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/iv"/> </LinearLayout>
界面非常简单,就是EditText、Button和ImageView垂直线性布局,用户通过输入网址,在ImageView中展示链接的图片。
下面,我们来看看Activity的源码:
package com.yzx.picview; 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.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_path; private ImageView iv; //1、创建handler private Handler handler = new Handler(){ //接受并处理消息 @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bitmap bm = (Bitmap) msg.obj; //把位图设置到ImageView控件 iv.setImageBitmap(bm); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取EditText控件和ImageView et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); } public void click(View v){ final String path = et_path.getText().toString().trim(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "请输入网络路径", 0).show(); }else{ new Thread(){ @Override public void run() { try { //从网络上下载图片,并显示在ImageView里 //1、创建URL,打开一个HTTP的连接; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置请求头信息 conn.setRequestMethod("GET"); conn.setReadTimeout(5000); //3、接收服务器端返回的响应数据,响应码:200 ok,404没有找到资源 ,503服务器端内部错误 int code = conn.getResponseCode(); if(code == 200){ InputStream is = conn.getInputStream(); //把接受的二进制数据转化成图片 Bitmap bm = BitmapFactory.decodeStream(is); //2.得到handler的引用,obtain方法从缓冲池中获取一个空的Message,如果缓冲池中没有就new一个 Message msg = Message.obtain(); msg.obj = bm; //把消息发送给住线程 handler.sendMessage(msg); /*//把位图设置到ImageView控件,只适用于Android2.3以下的版本 iv.setImageBitmap(bm);*/ }else{ Toast.makeText(MainActivity.this, "服务器返回数据错误", 0).show(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); } } }
这里只要就是要理解前面讲的消息处理机制
运行结果图:
相关文章推荐
- HDU 4975 A simple Gaussian elimination problem网络流
- 关于编写网络技术文档的基本步骤
- ACM学习历程—广东工业大学2016校赛决赛-网络赛C wintermelon的魔界寻路之旅(最短路 && 递推)
- 采用TCP协议的PIC32MZ ethernet bootloader
- HTTP协议格式
- HTTP报文结构
- HTTP 411 Content-Length required
- 编写新闻客户端&网络编程day1(66期第四天)
- ios解决http上传乱码问题
- Http请求头探讨
- 神经网络指南Hacker's guide to Neural Networks
- BP神经网络原理及编程实现
- 卷积神经网络Convolutional Neural Networks
- 深度卷积神经网络用于图像缩放Image Scaling using Deep Convolutional Neural Networks
- TCP漏洞:半连接
- 用matlab训练数字分类的深度神经网络Training a Deep Neural Network for Digit Classification
- TCP协议三次握手连接四次握手断开和DOS攻击
- 深度神经网络入门教程Deep Neural Networks: A Getting Started Tutorial
- 深度神经网络如何看待你,论自拍What a Deep Neural Network thinks about your #selfie
- nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a M