android 下载工具
2016-05-17 14:05
429 查看
public interface DownloadFile { public void download(String url, String destinationPath); public interface Listener{ public void onSuccess(String url, String destinationPath); public void onFailure(Exception e); public void onProgressUpdate(int progress, int total); } }
public class DownloadFileUrlConnectionImpl implements DownloadFile {
private static final int KILOBYTE = 1024;
private static final int BUFFER_LEN = 1 * KILOBYTE;
private static final int NOTIFY_PERIOD = 150 * KILOBYTE;
Context context;
Handler uiThread;
Listener listener = new NullListener();
public DownloadFileUrlConnectionImpl(Context context, Handler uiThread, Listener listener) {
this.context = context;
this.uiThread = uiThread;
this.listener = listener;
}
@Override
public void download(final String url, final String destinationPath) {
new Thread(new Runnable() {
@Override
public void run() {
try{
File file = new File(destinationPath);
FileOutputStream fileOutput = new FileOutputStream(file);
HttpURLConnection urlConnection = null;
URL urlObj = new URL(url);
urlConnection = (HttpURLConnection) urlObj.openConnection();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
int counter = 0;
byte[] buffer = new byte[BUFFER_LEN];
int bufferLength = 0;
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
while ( (bufferLength = in.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
counter += bufferLength;
if(listener != null && counter > NOTIFY_PERIOD){
notifyProgressOnUiThread(downloadedSize, totalSize);
counter = 0;
}
}
urlConnection.disconnect();
fileOutput.close();
}catch(MalformedURLException e){
notifyFailureOnUiThread(e);
}catch(IOException e){
notifyFailureOnUiThread(e);
}
notifySuccessOnUiThread(url, destinationPath);
}
}).start();
}
protected void notifySuccessOnUiThread(final String url, final String destinationPath) {
if(uiThread == null)
return;
uiThread.post(new Runnable() {
@Override
public void run() {
listener.onSuccess(url, destinationPath);
}
});
}
protected void notifyFailureOnUiThread(final Exception e){
if(uiThread == null)
return;
uiThread.post(new Runnable() {
@Override
public void run() {
listener.onFailure(e);
}
});
}
private void notifyProgressOnUiThread(final int downloadedSize, final int totalSize) {
if(uiThread == null)
return;
uiThread.post(new Runnable() {
@Override
public void run() {
listener.onProgressUpdate(downloadedSize, totalSize);
}
});
}
protected class NullListener implements Listener{
public void onSuccess(String url, String destinationPath) {}
public void onFailure(Exception e) {}
public void onProgressUpdate(int progress, int total) {}
}
}
相关文章推荐
- android studio怎么快速打出System.out.println()
- GitHub 优秀的 Android 开源项目
- 怎么向Android stutio中导入第三方.jar包
- android webView使用js/css实现夜间模式 长按识别图片以及二维码,网页可以上传图片
- com.orhanobut.logger开源框架查看日志
- Android 屏幕录像解决方案
- Android开发技术学习之popupwindow的弹窗实现
- 【Android】MTK Android 编译命令
- Android_Page Curl:Android平台上的翻书翻页
- ANdroid studio加载*.aar
- Android gif图片的解码与合成
- android之存储篇_SQLite数据库_让你彻底学会SQLite的使用
- Android onTouchEvent,onClick,onLongClick的return值注意事项
- 深入理解Android事件分发机制
- Android自定义控件(含简单栗子)
- Android ListView 点击事件无法获得焦点简析
- Android环境搭建
- Android中的第一个NDK的例子
- Android学习路线
- 视频直播 解决方案