Download.java
-
package com.vibexie.util;
-
-
import android.app.ProgressDialog;
-
import android.content.Context;
-
import android.os.Environment;
-
import android.os.Handler;
-
import android.os.Message;
-
-
import org.apache.http.HttpResponse;
-
import org.apache.http.client.HttpClient;
-
import org.apache.http.client.methods.HttpGet;
-
import org.apache.http.impl.client.DefaultHttpClient;
-
import org.apache.http.util.EntityUtils;
-
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
-
/**
-
* VibeXie created in 4.11.2015 @vibexie.com
-
*
-
* 下载文件的工具类
-
*
-
* 1.downloadAndReturnViaCallback方法可以选择保存文件,并且可以通过接口回调得到下载的文件
-
* 示例(下载图片并显示在ImageView中):
-
* Download download=new Download(MainActivity.this);
-
* download.downloadAndReturnViaCallback(url,true,"/tmp","tmp.jpg",new Download.DownloadCallback() {
-
* @Override
-
* public void callbackDownload(byte[] result) {
-
* Bitmap bitmap= BitmapFactory.decodeByteArray(result,0,result.length);
-
* imageView.setImageBitmap(bitmap);
-
* }
-
* });
-
*
-
* 2.justDownloadAndSave方法直接下载并保存文件到sd卡
-
*
-
* 3.注意添加网络和写sd卡权限
-
*/
-
public class Download {
-
-
private ProgressDialog progressDialog;
-
-
/**
-
* 构造函数
-
* @param context
-
*/
-
Download(Context context){
-
progressDialog=new ProgressDialog(context);
-
progressDialog.setTitle("提示");
-
progressDialog.setMessage("正在拼命加载...");
-
}
-
-
/**
-
* 回调接口
-
*/
-
public interface DownloadCallback{
-
public void callbackDownload(byte[] result);
-
}
-
-
/**
-
* 直接根据路径下载并保存
-
* @param internetPath 网络下载路径,字符串格式
-
* @param savePath 保存到本地存储卡的路径,注意是文件夹格式,如:/tmp
-
* @param fileName 存使用的文件名,格式如:tmp.jpg
-
*/
-
public void justDownloadAndSave(String internetPath,final String savePath,String fileName){
-
downloadAndReturnViaCallback(internetPath,true,savePath,fileName,null);
-
}
-
-
/**
-
* 下载保存并回调返回
-
* @param internetPath 网络下载路径
-
* @param save 是否保存到本地,若为false,savePath和fileName为null
-
* @param savePath 保存到本地存储卡的路径,注意是文件夹格式,如:/tmp
-
* @param fileName 保存使用的文件名,格式如:tmp.jpg
-
* @param downloadCallback 回调下载的文件
-
*/
-
public void downloadAndReturnViaCallback(final String internetPath, final boolean save,final String savePath, final String fileName, final DownloadCallback downloadCallback){
-
-
final Handler handler=new Handler() {
-
@Override
-
public void handleMessage(Message msg) {
-
super.handleMessage(msg);
-
-
/**
-
* 如果downloadCallback不为null,说明是直接调用downloadAndReturnViaCallback
-
* 而不是调用justDownloadAndSave,调用justDownloadAndSave则无需handle处理
-
*/
-
if(downloadCallback!=null){
-
byte[] result=(byte[])msg.obj;
-
downloadCallback.callbackDownload(result);
-
}
-
-
if(msg.what==1){
-
progressDialog.dismiss();
-
}
-
}
-
};
-
-
/**
-
* 下载线程类实现Runnable接口
-
*/
-
class MyThread implements Runnable{
-
@Override
-
public void run() {
-
HttpClient httpClient=new DefaultHttpClient();
-
HttpGet httpGet=new HttpGet(internetPath);
-
try {
-
HttpResponse httpResponse=httpClient.execute(httpGet);
-
if(httpResponse.getStatusLine().getStatusCode()==200){
-
byte[] result= EntityUtils.toByteArray(httpResponse.getEntity());
-
-
/**
-
* 保存文件
-
*/
-
if(save) {
-
saveFile(savePath,fileName,result);
-
}
-
-
Message message=Message.obtain();
-
-
/**
-
* 如果downloadCallback不为null,说明是直接调用downloadAndReturnViaCallback
-
* 而不是调用justDownloadAndSave,调用justDownloadAndSave则无需handle处理
-
*/
-
if(downloadCallback!=null){
-
message.obj=result;
-
}
-
-
/**
-
* 下载结束标志位,1表示结束
-
*/
-
message.what=1;
-
handler.sendMessage(message);
-
-
}
-
} catch (IOException e) {
-
e.printStackTrace();
-
}finally {
-
/**
-
* 关闭http连接
-
*/
-
httpClient.getConnectionManager().shutdown();
-
}
-
}
-
}
-
-
/**
-
* 启动下载线程
-
*/
-
new Thread(new MyThread()).start();
-
-
/**
-
* 显示Dialog
-
*/
-
progressDialog.show();
-
}
-
-
-
/**
-
* 保存文件
-
* @param savePath 下载路径
-
* @param result 要保存的byte[]流
-
*/
-
private void saveFile(String savePath,String fileName,byte[] result){
-
/**
-
* 判断sd卡是否存在,不存在直接返回
-
*/
-
if(Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTED)){
-
return;
-
}
-
/**
-
* 路径不存在则创建文件路径
-
*/
-
File tmp=new File(Environment.getExternalStorageDirectory()+savePath);
-
if(!tmp.exists()){
-
tmp.mkdirs();
-
}
-
-
File file=new File(tmp,fileName);
-
try{
-
OutputStream outputStream=new FileOutputStream(file);
-
outputStream.write(result,0,result.length);
-
outputStream.close();
-
}catch (Exception e){
-
e.printStackTrace();
-
}
-
}
- }