美文网首页
HttpURLConnection请求网络图片&&不依赖图片框架

HttpURLConnection请求网络图片&&不依赖图片框架

作者: MengkZhang | 来源:发表于2019-05-10 18:12 被阅读0次

图片显示的原理:mImageView.setImageBitmap(bitmap);
把服务器返回的InputStream数据写到一个文件file
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath())即可

在子线程请求图片数据

public void reqHttpWebPicShow(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String path = "https://img.haomeiwen.com/i5914881/8e0f627c5963bd0d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp";
                File  file = new File(getCacheDir(),Base64.encodeToString(path.getBytes(), Base64.DEFAULT));

                //使用缓存数据
                if (file.exists() && file.length() >0) {
                    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                    Message message = mHandler.obtainMessage();
                    message.obj = bitmap;
                    message.what = 4;
                    mHandler.sendMessage(message);
                    return;
                }
                //请求网络数据
                try {
                    URL url = new URL(path);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    int responseCode = httpURLConnection.getResponseCode();
                    if (responseCode == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        FileOutputStream fos = new FileOutputStream(file);
                        int len = -1;
                        byte[] buffer = new byte[1024];
                        while ((len = inputStream.read(buffer)) != -1) {
                            fos.write(buffer,0,len);
                        }
                        fos.close();
                        inputStream.close();

                        final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

                        Message message = mHandler.obtainMessage();
                        message.obj = bitmap;
                        message.what = 4;
                        mHandler.sendMessage(message);

//                        runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                mImageView.setImageBitmap(bitmap);
//                            }
//                        });

                    } else {

                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

在UI线程显示

    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1://成功
                    String s = (String) msg.obj;
                    Toast.makeText(HttpActivity.this, "" + s, Toast.LENGTH_SHORT).show();
                    break;
                case 2://失败
                    Toast.makeText(HttpActivity.this, "失败", Toast.LENGTH_SHORT).show();
                    break;
                case 3://异常
                    Toast.makeText(HttpActivity.this, "异常", Toast.LENGTH_SHORT).show();
                    break;


                case 4://获取显示bitmap
                    Bitmap bitmap = (Bitmap) msg.obj;
                    mImageView.setImageBitmap(bitmap);
                    break;
            }
        }
    };

程序源码:

https://github.com/MengkZhang/base_demo/tree/4c7dda19ce69512b8ccd5b8a88f559383f67ac03

相关文章

网友评论

      本文标题:HttpURLConnection请求网络图片&&不依赖图片框架

      本文链接:https://www.haomeiwen.com/subject/dnczoqtx.html