[关闭]
@tianyu-211 2016-12-27T10:52:25.000000Z 字数 2949 阅读 1519

安卓网络图片查看器

安卓


制作一个网络图片查看器

http请求图片的流程

代码实现步骤

  1. URL url = new URL(path);
  2. 通过url打开一个对应的连接httpUrlConnection();
  3. 设置一些初始化的参数,请求方法(GET,POST)请求头
  4. 获取服务器端响应conn.getResponseCode()
  5. 返回200 OK 400 资源找不到 503 服务器内部错误
  6. com。getInputStream();服务器返回的数据量流

因为需要访问网络,所以AndroidManifest.xml中需要添加网络权限
android stusio添加网络权限

网络在主线程的异常
NetWorkOnMainThreadException

从android4.0开始,为了让应用程序更加流畅,google强制要求访问网络的操作只能在子线程

activity的onCreate方法和点击事件这些方法都是运行在主线程里面(UI线程)

只有主线程可以修改应用程序的ui,其他线程修改的界面是不运行的

目录结构

Mainactivity

public class MainActivity extends AppCompatActivity {
 private EditText et_image;
    private ImageView iv;

    //这里导包需要导入andoird.os中的包
    private Handler handler = new Handler(){
        //在这里ctrl+o可以快速重写方法,选择handleMessage

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //用Bitmap接收msg
            Bitmap bitmap = (Bitmap) msg.obj;
            iv.setImageBitmap(bitmap);
        }
    };
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  
        et_image = (EditText) findViewById(R.id.et_image);
        iv = (ImageView) findViewById(R.id.iv);
        }
    }
     //查看图片
    public void image(View view){
        final String path = et_image.getText().toString().trim();
        if(TextUtils.isEmpty(path)){
            Toast.makeText(this,"图片路径不能为空",Toast.LENGTH_SHORT).show();
        }else {
            //安卓4.0以后的UI修改只能在子线程进行
            //所以新建一个子线程
            //new Thread(){}.start();
            new Thread(){
                @Override
                public void run() {
                    //下载网络上的图片,显示到imageView里
                    try {
                        //1.创建URL对象
                        URL url = new URL(path);
                        //2.通过url对象打开http连接
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        //conn设置请求方式为GET
                        conn.setRequestMethod("GET");
                        conn.setRequestProperty("Accept","text/php,*.png,*/*");
                        int code = conn.getResponseCode();
                        //200 ok 404文件不存在或者503服务器内部错误
                        if(code == 200){
                            //得到服务器返回的数据流
                            InputStream is = conn.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(is);
                            //iv.setImageBitmap(bitmap);
                            //由于子线程不可以直接修改ui
                            //这里是先获取数据后,调用主线程的handler发消息去更新ui
                            Message msg = new Message();
                            //把获取到的bitmap放到msg盒子里
                            msg.obj = bitmap;
                            handler.sendMessage(msg);
                        }else {
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="cn.guotianyu.musicplayer.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/et_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:hint="请输入网络图片路径"
            android:text="http://guotianyu.cn:211/mouse.png"/>
        <Button
            android:id="@+id/bt_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="image"
            android:text="查看"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff00ff"/>

</LinearLayout>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注