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. 【LeetCode】96 - Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  2. 禁止Windows安装软件

    今天电脑莫名安装上百度杀毒,想永久解决这个问题. 1.卸载百度杀毒 2.运行cmd-->sc delete 'service name' 3.sc delete BDMiniDlUpdate/B ...

  3. Java 断点调试总结

    为了准备调试,你需要在代码中设置一个断点先,以便让调试器暂停执行允许你调试,否则,程序会从头执行到尾,你就没有机会调试了. 1. 条件断点 断点大家都比较熟悉,在Eclipse Java 编辑区的行头 ...

  4. Linux中的.emacs文件

    刚开始的时候在Windows下使用emacs,那个时候配置 .emacs文件直接去C盘里\Users\(username)\AppData\Roaming 路径下查找就可以了(最开始的时候可以打开em ...

  5. RPC进阶篇

    RPC实现结构拆解 RPC过程调用详解:RPC 服务端通过 RpcServer 去导出(export)远程接口方法,而客户端通过 RpcClient 去引入(import)远程接口方法. 客户端像调用 ...

  6. ets dets

    相同点:ets和dets都提供“键—值”搜索表 不同点:ets驻留在内存,dets驻留在磁盘 特点:ets表和dets表可以被多个进程共享,因此通过这两个模块可以实现数据间的交换 一  ets表 实现 ...

  7. hip-hop初探

    啥都不说了,上两张图片先 1.使用hiphop的 2.不使用这玩意的 都是前端部署nginx,转发的后面php的 hhvm的配置文件 /etc/hhvm.hdf 目前结论:facebook的这玩意可能 ...

  8. CodeForces 709B Checkpoints (数学,最短路)

    题意:给定你的坐标,和 n 个点,问你去访问至少n-1个点的最短路是多少. 析:也是一个很简单的题,肯定是访问n-1个啊,那么就考虑从你的位置出发,向左访问和向右访问总共是n-1个,也就是说你必须从1 ...

  9. URAL 2070 Interesting Numbers (找规律)

    题意:在[L, R]之间求:x是个素数,因子个数是素数,同时满足两个条件,或者同时不满足两个条件的数的个数. 析:很明显所有的素数,因数都是2,是素数,所以我们只要算不是素数但因子是素数的数目就好,然 ...

  10. sql语句增删改查(转)

    一.增:有4种方法 1.使用insert插入单行数据:                  语法:insert [into] <表名> [列名] values <列值>    例 ...