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. Android百度地图开发(五)公交线路详情搜索、多条线路显示

    一.公交线路详情检索 获取公交线路的详情主要分来两步,1.获取公交线路的Uid,2.通过Uid获取公交线路详情. 1.获取公交线路的Uid: /* * 获得公交线路图的Uid,并且根据系Uid发起公交 ...

  2. cdh5.4、cm5.4 安装详细步骤

    安装准备: 1.centos6.5 64位 虚拟机,内存分配4G.硬盘位20G 2.cm5.4 cdh5.4 包 安装步骤 一.centos安装完后,进行系统配置 1.关闭防火墙 service ip ...

  3. C#语言基础01

    Console.WriteLine("hello"); Console.ReadKey();// 按一个按键继续执行 string s=Console.ReadLine();//用 ...

  4. LRU Cache的实现

      代码如下:     来自为知笔记(Wiz)

  5. 原生JS取代一些JQuery方法

    1.选取元素 // jQuery var els = $('.el'); // Native var els = document.querySelectorAll('.el'); // Shorth ...

  6. xdebug初步

    ;加载xdebug模块. 根据PHP版本来选择是zend_extension还是zend_extension_ts  ts代表线程安全  被坑过1次zend_extension="\web\ ...

  7. 使用arm开发板搭建无线mesh网络(二)

    上篇博文介绍了无线mesh网络和adhoc网络的区别,这篇文章将介绍无线mesh网络的骨干网节点的组建过程.首先需要介绍下骨干网节点的设计方案:每个骨干网节点都是由一块友善之臂的tiny6410 ar ...

  8. VS2010 删除空行

    查找内容:^:b*$\n 替换为: 查找范围:当前文档 使用:正则表达式 vs2013 ^\s*(?=\r?$)\n

  9. 恢复HDFS误删数据

    [恢复HDFS误删数据] HDFS会为每一个用户创建一个回收站目录:/user/用户名/.Trash/,每一个被用户通过Shell删除的文件/目录,在系统回收站中都一个周期,也就是当系统回收站中的文件 ...

  10. C++11对象构造的改良

    [C++11对象构造的改良] C++03中一个构造函数无法构造另一个构造函数,因为A()实际上意味着生成一个临时对象,存在语音混淆.详情请看参考2. C++11中允许直接在初始化列表中调用其它的构造函 ...