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网络通信--网络图片查看的更多相关文章

  1. Android 网络图片查看器

    今天来实现一下android下的一款简单的网络图片查看器 界面如下: 代码如下: <LinearLayout xmlns:android="http://schemas.android ...

  2. 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)

    1.内容观察者ContentObserver 如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,可以在ContentProvider 发生数据变化时调 ...

  3. android 网络_网络图片查看器

    xml <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...

  4. Android -- 网络图片查看器,网络html查看器, 消息机制, 消息队列,线程间通讯

    1. 原理图 2. 示例代码 (网络图片查看器) (1)  HttpURLConnection (2) SmartImageView (开源框架:https://github.com/loopj/an ...

  5. 黎活明8天快速掌握android视频教程--23_网络通信之网络图片查看器

    1.首先新建立一个java web项目的工程.使用的是myeclipe开发软件 图片的下载路径是http://192.168.1.103:8080/lihuoming_23/3.png 当前手机和电脑 ...

  6. Android简易实战教程--第二十六话《网络图片查看器在本地缓存》

    本篇接第二十五话  点击打开链接   http://blog.csdn.net/qq_32059827/article/details/52389856 上一篇已经把王略中的图片获取到了.生活中有这么 ...

  7. Android简易实战教程--第二十五话《网络图片查看器》

    访问网络已经有了很成熟的框架.这一篇只是介绍一下HttpURLConnection的简单用法,以及里面的"注意点".这一篇可以复习或者学习HttpURLConnection.han ...

  8. Android 网络图片查看器与网页源码查看器

    在AndroidManifest.xml里面先添加访问网络的权限: <uses-permission android:name="android.permission.INTERNET ...

  9. Android项目——网络图片查看器

    效果-=-------------->加入包 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an ...

随机推荐

  1. 在logopond中看到的优秀设计随想

    本随笔仅仅只是自己对于设计作品的想法,不喜勿喷~ 昨日看到关于大神配色的文章,决定在logopond网站中看看优秀的作品,以为自己的配色找找灵感,学习学习,对自己有很强的震撼力的有: 以女性高跟性的抽 ...

  2. The Stereo Action Dimension

    Network MIDI on iOS - Part 1   This is an app I wrote to try out some ideas for networked MIDI on iP ...

  3. 怎么监视跟踪一个进程(Process)中的MS Unit Test DLL的详细性能(performance)【asp.net C#】

    Sample This tutorial will show how to instrument a unit test DLL for performance profiling. Visual S ...

  4. js运动 模仿淘宝幻灯

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. static_cast, dynamic_cast, const_cast探讨

    转自:http://www.cnblogs.com/chio/archive/2007/07/18/822389.html 首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 ...

  6. HD1085 Holding Bin-Laden Captive!

    Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for ...

  7. Lucene/ElasticSearch 学习系列 (1) 为什么学,学什么,怎么学

    为什么学 <What I wish I knew When I was 20>这本书给了我很多启发.作者在书中提到,Stanford 大学培养人才的目标是 ”T形人才“:精通某个领域,但对 ...

  8. Common Converters in WPF/Silverlight

    using System; using System.ComponentModel; using System.Globalization; using System.Windows.Data; na ...

  9. 最基本的Unix系统操作命令

    基本知识点: OSX 采用的Unix文件系统,所有文件都挂在跟目录 / 下面,所以不在要有Windows 下的盘符概念. 你在桌面上看到的硬盘都挂在 /Volumes 下. 比如接上个叫做 USBHD ...

  10. shell切换用户执行后面语句 su与su -的区别

    关于su和su -的区别,切换用户是可以使用su tom或者su - tom来实现,但是两者有区别,su只是切换身份,但shell环境仍然是原用户的shell,su -是连用户的shell环境一起切换 ...