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
- android-原生图片下载的对比
- PhoneGap对比原生android应用程序
- android下的图片下载方法
- Android 图片下载本地内存的缓存方式
- Android 图片下载本地内存的缓存方式
- Android-->网络图片下载的三级缓存策略
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- picasso_强大的Android图片下载缓存库
- picasso-强大的Android图片下载缓存库
- ld: framework not found ReactiveCocoa clang: error: linker command failed with exit code 1
- 去除快捷方式小箭头
- jdk配置
- Web Service 的介绍及入门程序
- 商品展示(二)
- android-原生图片下载的对比
- 关于nil和 null和NSNull的区别及相关问题
- You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletio
- es6的 ... 三点运算符的用法
- Android相框合成图片抠图
- Android开源特效常用链接大集合
- JAXB转XML: 错误 class nor any of its super class is known to this 的处理
- Ubuntu 16.04 LTS 下安装MatLab2015b 以及Matlab system error解决办法
- Xcode常见编译失败问题解决方案