Http网络通信--网络图片查看

本文介绍如何在Android应用中实现网络图片的加载,并解决了在不同版本Android系统中可能遇到的ANR问题,通过使用子线程及Handler进行UI更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();

}

}

}


 

 

 

效果



 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值