zoukankan      html  css  js  c++  java
  • Http网络通信--网络图片查看

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

    }

    }

    }

     

     

     

    效果


     

     

     

  • 相关阅读:
    「CSP-S 2019」初赛解析
    「SP741」STEAD
    「CF1382B」Sequential Nim
    「二分图」学习笔记
    题解 P3321 【[SDOI2015]序列统计】
    题解 P5339 【[TJOI2019]唱、跳、rap和篮球】
    题解 P3200 【[HNOI2009]有趣的数列】
    题解 P2606 【[ZJOI2010]排列计数】
    题解 CF1095F 【Make It Connected】
    题解 CF1155E 【Guess the Root】
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3397924.html
Copyright © 2011-2022 走看看