Http网络通信--网络图片查看
1.要在andorid中实现网络图片查看,涉及到用户隐私问题,所以要在AndroidManifest.xml中添加访问网络权限
<uses-permission android:name="android.permission.INTERNET"/>
2.布局文件
<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:layout_weight="200"
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<EditText
android:id="@+id/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入浏览地址"
android:text="http://10.162.0.171:8080/Image/iamge.jpg"
/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="浏览图片"
android:onClick="onClick"
/>
</LinearLayout>
3.MainActivity.java
package com.example.showimage;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView image;
private EditText path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
path = (EditText) findViewById(R.id.path);
}
public void onClick(View view) throws IOException{
String imagePath = path.getText().toString();
if(TextUtils.isEmpty(imagePath)){
Toast.makeText(MainActivity.this, "图片路径不能为空", Toast.LENGTH_LONG).show();
}else{
URL url = new URL(imagePath);
//根据url发送http请求
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
//设置请求方式
conn.setRequestMethod("GET");
//设置连接时间
conn.setConnectTimeout(5000);
//响应编码
int code = conn.getResponseCode();
if(code==200){
//得到输入流
InputStream is=conn.getInputStream();
//位图
Bitmap bitmap=BitmapFactory.decodeStream(is);
image.setImageBitmap(bitmap);
}else{
Toast.makeText(MainActivity.this, "图片路径不能为空", Toast.LENGTH_LONG).show();
}
}
}
}
在4.0以上版本的模拟器上运行以上代码,会抛出如下错误
10-30 02:05:28.418: E/AndroidRuntime(577): Caused by: android.os.NetworkOnMainThreadException
在这,引入一个anr的概念:
Anr :application not response 应用程序无响应
导致anr的原因:主线程需要做好多的事情,如:响应点击事件,更新UI
所以如果在主线程里面阻塞时间过长,应用程序就无响应
解决办法:为了避免出现anr,把所有耗时的操作放在子线程里面执行
出现以上的原因是4.0以上的模拟器不允许网络的操作在主线程里。而2.3版本的就没有这样的设置。
所以为了上程序无论在什么版本下都可以运行,做法就是把访问网络图片放进子线程里面执行
修改MainActivity.java
package com.example.showimage;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView image;
private EditText path;
private final int MESSAGE1=1;
private final int MESSAGE2=2;
//主线程创建消息处理器
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==MESSAGE1){
Bitmap bitmap =(Bitmap) msg.obj;
image.setImageBitmap(bitmap);//这是修改ui
}else if(msg.what==MESSAGE2){
Toast.makeText(MainActivity.this, "显示图片错误", Toast.LENGTH_LONG).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
path = (EditText) findViewById(R.id.path);
}
public void onClick(View view) throws IOException{
final String imagePath = path.getText().toString();
if(TextUtils.isEmpty(imagePath)){
Toast.makeText(MainActivity.this, "图片路径不能为空", Toast.LENGTH_LONG).show();
}else{
new Thread(){
@Override
public void run() {
try{
URL url = new URL(imagePath);
//根据url发送http请求
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
//设置请求方式
conn.setRequestMethod("GET");
//设置连接时间
conn.setConnectTimeout(5000);
//响应编码
int code = conn.getResponseCode();
if(code==200){
//得到输入流
InputStream is=conn.getInputStream();
//位图
Bitmap bitmap=BitmapFactory.decodeStream(is);
//告诉主线程,帮我修改ui
Message msg = new Message();
msg.what=MESSAGE1; //handler处理的标志
msg.obj=bitmap; //将位图传给handler处理
handler.sendMessage(msg);//发送消息
//image.setImageBitmap(bitmap);//这是修改ui
}else{
//告诉主线程,帮我修改ui
Message msg = new Message();
msg.what=MESSAGE2; //handler处理的标志
handler.sendMessage(msg);//发送消息
//Toast在主线程显示,也需要放进子线程中
//Toast.makeText(MainActivity.this, "显示图片错误", Toast.LENGTH_LONG).show();
}
}catch(Exception e){
e.printStackTrace();
Message msg = new Message();
msg.what=MESSAGE2; //handler处理的标志
handler.sendMessage(msg);//发送消息
}
}
}.start();
}
}
}
效果
最新文章
- C#中MessageBox用法大全
- DHCP snooping
- “Ceph浅析”系列之一——前言
- Apache Spark技术实战之7 -- CassandraRDD高并发数据读取实现剖析
- Effective C++笔记:资源管理
- IOS开发证书变成“此证书的签发者无效”解决方法
- POJ3525 Most Distant Point from the Sea(半平面交)
- 利用URLRewriter重写url地址
- URAL 1994 The Emperor&#39;s plan 求组合数 大数用log+exp处理
- &;nbsp; 与 空格的区别
- [转]百度地图点聚合MarkerClusterer移动地图时,Marker的Label丢失的问题
- 从PyOpenCV到CV2
- MDK常见错误详解集合
- factorOne cannot be&;nb…
- 春招实习面经分享(已拿到腾讯春招Offer)
- Mysql之SQL经验基础积累
- pip安装scrapy失败:twisted安装失败 error: Microsoft Visual C++ 14.0 is required.. 解决方法
- JS学习记录------JS基本指令
- luogu1196 银河英雄传说 (并查集)
- 读懂jquery