android-原生图片下载的对比

HttpClient +thread+handleMessage:

 /*------------------HttpClient -----------*/
//主线程中执行:
 new Thread(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);

                Bitmap bitmap = getHttpGetBitmap(url);
                Message message = Message.obtain();
                message.obj = bitmap;
                message.what = 1;
                handler.sendMessage(message);

                handler.sendEmptyMessage(2);
            }
        }).start();//注意start方法


//不可在子线程中new
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    pDialog.show();
                    break;
                case 1:
                    iv1.setImageBitmap((Bitmap) msg.obj);
                case 2:
                    pDialog.dismiss();
                    break;
            }
        }
    };


    /**
     * httpClient
     *
     * @param url
     * @return
     */
    public static Bitmap getHttpGetBitmap(String url) {
        try {
            Bitmap bitmap = null;
            // 新建一个默认的连接
            HttpClient client = new DefaultHttpClient();
            // 新建一个Get方法
            HttpGet get = new HttpGet(url);
            // 得到网络的回应
            HttpResponse response = null;
            response = client.execute(get);
            // 如果服务器响应的是OK的话!
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                InputStream is = response.getEntity().getContent();
                bitmap = BitmapFactory.decodeStream(is);

                is.close();
            }
            return bitmap;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



httpUrlConnection+thread+handler.post:

/*-----------------------httpUrlConnection------------------------*/
//主线程中执行:
  new Thread(new Runnable() {
            @Override
            public void run() {
                final Bitmap bitmap = getNetWorkBitmap(url);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iv2.setImageBitmap(bitmap);
                    }
                });
            }
        }).start();    


/**
     * httpUrlConnection
     *
     * @param urlString
     * @return
     */
    public static Bitmap getNetWorkBitmap(String urlString) {
        URL imgUrl = null;
        Bitmap bitmap = null;
        try {
            imgUrl = new URL(urlString);
            // 使用HttpURLConnection打开连接
            HttpURLConnection urlConn = (HttpURLConnection) imgUrl.openConnection();
            urlConn.setDoInput(true);
            urlConn.connect();
            // 将得到的数据转化成InputStream
            InputStream is = urlConn.getInputStream();
            // 将InputStream转换成Bitmap
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (MalformedURLException e) {
            System.out.println("[getNetWorkBitmap->]MalformedURLException");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("[getNetWorkBitmap->]IOException");
            e.printStackTrace();
        }
        return bitmap;
    }

AsyncTask+httpclient:

/*---------------------AsyncTask--------------------------*/
//主线程中执行:
 // 发送异步请求
        new MyAsyncTask(this).execute(url);



    /**
     * AsyncTask:
     */
    class MyAsyncTask extends AsyncTask<String, Void, byte[]> {

        private ProgressDialog pDialog;

        public MyAsyncTask(Context context) {
            pDialog = new ProgressDialog(context);
            pDialog.setIcon(R.mipmap.ic_launcher);
            pDialog.setTitle("提示");
            pDialog.setMessage("The data is loading...");
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog.show();
        }

        @Override
        protected byte[] doInBackground(String... strings) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(strings[0]);
                HttpResponse response = client.execute(get);

                if (response.getStatusLine().getStatusCode() == 200) {
                    byte[] datas = EntityUtils.toByteArray(response.getEntity());
                    // 缓存工具类:保存图片
//                    Utilsfile.saveImage(params[0], datas);// url:params[0]
                    return datas;
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(byte[] bytes) {
            super.onPostExecute(bytes);
            if (bytes != null) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                iv3.setImageBitmap(bitmap);
            }
            pDialog.dismiss();
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值