Chinaunix首页 | 论坛 | 博客
  • 博客访问: 417550
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Android平台

2015-04-11 21:13:47


Download.java

  1. package com.vibexie.util;

  2. import android.app.ProgressDialog;
  3. import android.content.Context;
  4. import android.os.Environment;
  5. import android.os.Handler;
  6. import android.os.Message;

  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.methods.HttpGet;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. import org.apache.http.util.EntityUtils;

  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.io.OutputStream;

  16. /**
  17.  * VibeXie created in 4.11.2015 @vibexie.com
  18.  *
  19.  * 下载文件的工具类
  20.  *
  21.  * 1.downloadAndReturnViaCallback方法可以选择保存文件,并且可以通过接口回调得到下载的文件
  22.  * 示例(下载图片并显示在ImageView中):
  23.  * Download download=new Download(MainActivity.this);
  24.  * download.downloadAndReturnViaCallback(url,true,"/tmp","tmp.jpg",new Download.DownloadCallback() {
  25.  * @Override
  26.  * public void callbackDownload(byte[] result) {
  27.  * Bitmap bitmap= BitmapFactory.decodeByteArray(result,0,result.length);
  28.  * imageView.setImageBitmap(bitmap);
  29.  * }
  30.  * });
  31.  *
  32.  * 2.justDownloadAndSave方法直接下载并保存文件到sd卡
  33.  *
  34.  * 3.注意添加网络和写sd卡权限
  35.  */
  36. public class Download {

  37.     private ProgressDialog progressDialog;

  38.     /**
  39.      * 构造函数
  40.      * @param context
  41.      */
  42.     Download(Context context){
  43.         progressDialog=new ProgressDialog(context);
  44.         progressDialog.setTitle("提示");
  45.         progressDialog.setMessage("正在拼命加载...");
  46.     }

  47.     /**
  48.      * 回调接口
  49.      */
  50.     public interface DownloadCallback{
  51.         public void callbackDownload(byte[] result);
  52.     }

  53.     /**
  54.      * 直接根据路径下载并保存
  55.      * @param internetPath 网络下载路径,字符串格式
  56.      * @param savePath 保存到本地存储卡的路径,注意是文件夹格式,如:/tmp
  57.      * @param fileName 存使用的文件名,格式如:tmp.jpg
  58.      */
  59.     public void justDownloadAndSave(String internetPath,final String savePath,String fileName){
  60.         downloadAndReturnViaCallback(internetPath,true,savePath,fileName,null);
  61.     }

  62.     /**
  63.      * 下载保存并回调返回
  64.      * @param internetPath 网络下载路径
  65.      * @param save 是否保存到本地,若为false,savePath和fileName为null
  66.      * @param savePath 保存到本地存储卡的路径,注意是文件夹格式,如:/tmp
  67.      * @param fileName 保存使用的文件名,格式如:tmp.jpg
  68.      * @param downloadCallback 回调下载的文件
  69.      */
  70.     public void downloadAndReturnViaCallback(final String internetPath, final boolean save,final String savePath, final String fileName, final DownloadCallback downloadCallback){

  71.         final Handler handler=new Handler() {
  72.             @Override
  73.             public void handleMessage(Message msg) {
  74.                 super.handleMessage(msg);

  75.                 /**
  76.                  * 如果downloadCallback不为null,说明是直接调用downloadAndReturnViaCallback
  77.                  * 而不是调用justDownloadAndSave,调用justDownloadAndSave则无需handle处理
  78.                  */
  79.                 if(downloadCallback!=null){
  80.                     byte[] result=(byte[])msg.obj;
  81.                     downloadCallback.callbackDownload(result);
  82.                 }

  83.                 if(msg.what==1){
  84.                     progressDialog.dismiss();
  85.                 }
  86.             }
  87.         };

  88.         /**
  89.          * 下载线程类实现Runnable接口
  90.          */
  91.         class MyThread implements Runnable{
  92.             @Override
  93.             public void run() {
  94.                 HttpClient httpClient=new DefaultHttpClient();
  95.                 HttpGet httpGet=new HttpGet(internetPath);
  96.                 try {
  97.                     HttpResponse httpResponse=httpClient.execute(httpGet);
  98.                     if(httpResponse.getStatusLine().getStatusCode()==200){
  99.                         byte[] result= EntityUtils.toByteArray(httpResponse.getEntity());

  100.                         /**
  101.                          * 保存文件
  102.                          */
  103.                         if(save) {
  104.                             saveFile(savePath,fileName,result);
  105.                         }

  106.                         Message message=Message.obtain();

  107.                         /**
  108.                          * 如果downloadCallback不为null,说明是直接调用downloadAndReturnViaCallback
  109.                          * 而不是调用justDownloadAndSave,调用justDownloadAndSave则无需handle处理
  110.                          */
  111.                         if(downloadCallback!=null){
  112.                             message.obj=result;
  113.                         }

  114.                         /**
  115.                          * 下载结束标志位,1表示结束
  116.                          */
  117.                         message.what=1;
  118.                         handler.sendMessage(message);

  119.                     }
  120.                 } catch (IOException e) {
  121.                     e.printStackTrace();
  122.                 }finally {
  123.                     /**
  124.                      * 关闭http连接
  125.                      */
  126.                     httpClient.getConnectionManager().shutdown();
  127.                 }
  128.             }
  129.         }

  130.         /**
  131.          * 启动下载线程
  132.          */
  133.         new Thread(new MyThread()).start();

  134.         /**
  135.          * 显示Dialog
  136.          */
  137.         progressDialog.show();
  138.     }


  139.     /**
  140.      * 保存文件
  141.      * @param savePath 下载路径
  142.      * @param result 要保存的byte[]流
  143.      */
  144.     private void saveFile(String savePath,String fileName,byte[] result){
  145.         /**
  146.          * 判断sd卡是否存在,不存在直接返回
  147.          */
  148.         if(Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTED)){
  149.             return;
  150.         }
  151.         /**
  152.          * 路径不存在则创建文件路径
  153.          */
  154.         File tmp=new File(Environment.getExternalStorageDirectory()+savePath);
  155.         if(!tmp.exists()){
  156.             tmp.mkdirs();
  157.         }

  158.         File file=new File(tmp,fileName);
  159.         try{
  160.             OutputStream outputStream=new FileOutputStream(file);
  161.             outputStream.write(result,0,result.length);
  162.             outputStream.close();
  163.         }catch (Exception e){
  164.             e.printStackTrace();
  165.         }
  166.     }
  167. }

阅读(1419) | 评论(0) | 转发(0) |
0

上一篇:webView基础

下一篇:解析json工具类

给主人留下些什么吧!~~