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();
}
}
}
效果
Http网络通信--网络图片查看的更多相关文章
- Android 网络图片查看器
今天来实现一下android下的一款简单的网络图片查看器 界面如下: 代码如下: <LinearLayout xmlns:android="http://schemas.android ...
- 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)
1.内容观察者ContentObserver 如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,可以在ContentProvider 发生数据变化时调 ...
- android 网络_网络图片查看器
xml <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...
- Android -- 网络图片查看器,网络html查看器, 消息机制, 消息队列,线程间通讯
1. 原理图 2. 示例代码 (网络图片查看器) (1) HttpURLConnection (2) SmartImageView (开源框架:https://github.com/loopj/an ...
- 黎活明8天快速掌握android视频教程--23_网络通信之网络图片查看器
1.首先新建立一个java web项目的工程.使用的是myeclipe开发软件 图片的下载路径是http://192.168.1.103:8080/lihuoming_23/3.png 当前手机和电脑 ...
- Android简易实战教程--第二十六话《网络图片查看器在本地缓存》
本篇接第二十五话 点击打开链接 http://blog.csdn.net/qq_32059827/article/details/52389856 上一篇已经把王略中的图片获取到了.生活中有这么 ...
- Android简易实战教程--第二十五话《网络图片查看器》
访问网络已经有了很成熟的框架.这一篇只是介绍一下HttpURLConnection的简单用法,以及里面的"注意点".这一篇可以复习或者学习HttpURLConnection.han ...
- Android 网络图片查看器与网页源码查看器
在AndroidManifest.xml里面先添加访问网络的权限: <uses-permission android:name="android.permission.INTERNET ...
- Android项目——网络图片查看器
效果-=-------------->加入包 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an ...
随机推荐
- 【LeetCode】120 - Triangle
原题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...
- Python编程中的反模式
Python是时下最热门的编程语言之一了.简洁而富有表达力的语法,两三行代码往往就能解决十来行C代码才能解决的问题:丰富的标准库和第三方库,大大节约了开发时间,使它成为那些对性能没有严苛要求的开发任务 ...
- mysql 经典题目
题目1:实现如下效果 CREATE TABLE IF NOT EXISTS tb_amount( `Id` INT NOT NULL AUTO_INCREMENT, `), `), `Amount` ...
- 我的新计划 《2Dof Racing Simulator》2014/3/9 20:30:00
最近好久都没来网站上了,也没心思和时间去弄VellLock和升级V&View了.一直在蕴量这做一件大玩意. 最近一直都很忙,忙着做数电课设,还有各种实验,可是我的心思不在这些东西上,当然除了数 ...
- 安装phonegap
1.安装nodejs http://nodejs.org/ 2.安装git www.git-scm.com/download 3.安装jdk http://www.oracle.com/technet ...
- angular的directive笔记
原贴地址 1,tansclude: 是指令能够能够把外部定义的内容传回指令模板内部(通过在内部标签使用ng-transclude).这个外部指定的内容是根据外部的作用域控制的,跟指令的作用域无关.这个 ...
- Skeletal Animation
[Skeletal Animation] Skeletal animation is the use of “bones” to animate a model. The movement of bo ...
- vim编辑十六进制文件
首先用二进制方式打开 vim file -b 之后输入 :%!xxd 还原为二进制文件 :%!xxd -r
- Python的高级Git库 Gittle
Gittle是一个高级纯python git 库.构建在dulwich之上,提供了大部分的低层机制 Gittle是一个高级纯python git 库.构建在dulwich之上,提供了大部分的低层机制. ...
- ecshop读写分离
1.配置文件设置 $db_name = "ecshop"; $prefix = "ecs_"; $timezone = "Europe/Berlin& ...