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. ISO-8859-1

    ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII一致,0x80-0x9F之间是控制字符,0xA0-0xFF之间是文字符号 ...

  2. C# 实现无焦点窗体(转载)

    #region 无焦点窗体 [System.Runtime.InteropServices.DllImport("user32.dll")] private extern stat ...

  3. Lucene 入门需要了解的东西

    全文搜索引擎的原理网上大段的内容,要想深入的学习,最好的办法就是先用一下,lucene 发展比较快,下面是写第一个demo  要注意的一些事情: 1.Lucene的核心jar包,下面几个包分别位于不同 ...

  4. Array.prototype.slice.call

    Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组 ,::'age'}; Array.prototype.slice.call(arr); ...

  5. flex中文说明手册

    http://help.adobe.com/zh_CN/Flex/4.0/UsingFlashBuilder/WS6f97d7caa66ef6eb1e63e3d11b6c4d0d21-7f07.htm ...

  6. django部署到最后 主页上出现的坏请求解决办法

    ALLOWED_HOSTS = ['*'] 不然会出现400的坏请求 到此为止 环境总算配置完毕历时2天半重新熟悉了大量apache 和 linux下的命令

  7. JVM内存的那些事

    前言 对于C语言开发的程序员来说,在内存管理方面,必须负责每一个对象的生命周期,从有到无. 对于Java程序员你来说,在虚拟机内存管理的帮助下,不需要为每个new对象都匹配free操作,内存泄露和内存 ...

  8. 【MySql】权限不足导致的无法连接到数据库以及权限的授予和撤销

    [环境参数] 1.Host OS:Win7 64bit 2.Host IP:192.168.10.1 3.VM: VMware 11.1.0 4.Client OS:CentOS 6 5.Client ...

  9. c#与vb.net在App_Code里面编译要通过,需要以下web.config的配置

    web.config的配置: <system.web> <codeSubDirectories> <add directoryName="VB"/&g ...

  10. oracle学习 七 拼接变量+日期函数(持续更)

    select count(KEYCODE) from STHSGDOC.ZJSJJL where ysrq=to_date(to_char(sysdate,'yyyy')||'/1','yyyy/MM ...