Android学习之查看网络图片

在这里小编学习了查看网络图片的小案例,:

初始界面:

点击浏览后,效果如下:

需要注意的是 该案例需要获取联网权限,即:

<uses-permission android:name="android.permission.INTERNET"/>

 

具体步骤如下:

1.定义并初始化控件:

private EditText etImageUrl;

    private ImageView ivImage;
/**控件初始化*/

    private void init() {

        ivImage = (ImageView) findViewById(R.id.ivImage);

        etImageUrl = (EditText) findViewById(R.id.etImageUrl);

        

    }

2.建立Handler对象:

private Handler handler = new Handler(){

        public void handleMessage(android.os.Message msg) {

            //对接收到的消息进行处理

//            Looper.prepare();

            switch (msg.what) {

            case SHOW_IMAGE:

                Bitmap bitmap = (Bitmap) msg.obj;//将OS流中的Object强制转换为Bitmap对象

                ivImage.setImageBitmap(bitmap);

                

                Toast toast = Toast.makeText(MainActivity.this, "图片加载成功", Toast.LENGTH_SHORT);

                toast.setGravity(0, 0, 100);

                toast.show();

                break;



            default:

                break;

            }

        };

    };

3.设置显示网络图片的方法:

public void showImage(View view){//若在xml文件中使用onClick()属性,则其方法格式必须为  方法名(View view)

        final String path = etImageUrl.getText().toString();

        if (TextUtils.isEmpty(path)) {

            Toast.makeText(MainActivity.this, "图片路径不能为空", Toast.LENGTH_SHORT).show();

        } else {

            new Thread(){

                public void run(){

                    

                    //链接服务器,请求获取图片

                    try {

                        URL url = new URL(path);

                        //发出http请求

                        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

                        httpURLConnection.setRequestMethod("GET");

                        //设置链接超时时间

                        httpURLConnection.setConnectTimeout(5000);

                        

                        int responsecode = httpURLConnection.getResponseCode();//获取响应码

                        if (responsecode == 200) {

                            InputStream is = httpURLConnection.getInputStream();//获取输入流

                            Bitmap bitmap = BitmapFactory.decodeStream(is);

                            

                            //2.告诉主线程,帮我更新界面,内容是Bitmap

                            Message msg = new Message();

                            msg.what = SHOW_IMAGE;

                            msg.obj = bitmap;

                            handler.sendMessage(msg);

                        }else{

                            Toast toast = Toast.makeText(MainActivity.this, "图片加载失败", Toast.LENGTH_SHORT);

                            toast.setGravity(0, 0, 100);

                            toast.show();

                        }

                        

                    } catch (MalformedURLException e) {

                        e.printStackTrace();

                    } catch (IOException e) {

                        e.printStackTrace();

                    }

                }

            }.start();//务必启动线程

        }

    }

至此,查看网络图片的小案例就完成了.

你可能感兴趣的:(Android学习)