android-原生图片下载的对比

来源:互联网 发布:测试网络摄像头软件 编辑:程序博客网 时间:2024/06/05 05:52

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 0