Created
September 18, 2012 03:28
-
-
Save nalabjp/3741078 to your computer and use it in GitHub Desktop.
Acync simple file downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nalabjp.android; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.List; | |
import java.util.Map; | |
import android.os.AsyncTask; | |
import android.os.Environment; | |
import android.util.Log; | |
public class Downloader extends AsyncTask<String, Integer, File>{ | |
private static final String LOG_TAG = "Downloader"; | |
private boolean mIsNowDownloding = false; | |
private Throwable mThrowable; | |
private CallbackListener mListener; | |
public static interface CallbackListener { | |
public void onPreExecute(); | |
public void onProgressUpdate(Integer... progress); | |
public void onPostExecute(File file); | |
public void onCancelled(File file); | |
public void onCatchException(Throwable t); | |
} | |
public void setCallbackListener(CallbackListener l) { | |
mListener = l; | |
} | |
@Override | |
protected File doInBackground(String... params) { | |
File file = null; | |
try { | |
final URL url = new URL(params[0]); | |
final URLConnection conn = url.openConnection(); | |
final InputStream in = conn.getInputStream(); | |
Map<String, List<String>> headers = conn.getHeaderFields(); | |
String contentDisposition = headers.get("Content-Disposition").get(0); | |
String fileName = contentDisposition.substring(contentDisposition.indexOf("filename=")+"filename=".length()).replaceAll("\"", ""); | |
if (params.length > 1 && null != params[1] && 0 != params[1].length()) { | |
file = new File(params[1], fileName); | |
} else { | |
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName); | |
} | |
try { | |
final OutputStream out = new FileOutputStream(file, true); | |
try { | |
final int length = conn.getContentLength(); | |
final byte[] bytes = new byte[4096]; | |
float loaded = 0; | |
float nowload = 0; | |
mIsNowDownloding = true; | |
while(!Thread.interrupted()){ | |
int ret = in.read(bytes); | |
if(ret <= 0) { | |
break; | |
} | |
out.write(bytes, 0, ret); | |
nowload = loaded + ret; | |
loaded += ret; | |
publishProgress(Integer.valueOf((int)(nowload/length*100))); | |
} | |
} finally { | |
mIsNowDownloding = false; | |
out.close(); | |
} | |
} finally { | |
in.close(); | |
} | |
} catch (Throwable t) { | |
Log.e(LOG_TAG, "Uncaught exception is occured in progress download.", t); | |
mThrowable = t; | |
} | |
return file; | |
} | |
@Override | |
protected void onPreExecute() { | |
if (mListener != null) { | |
mListener.onPreExecute(); | |
} | |
} | |
@Override | |
protected void onProgressUpdate(final Integer... progress) { | |
if (mListener != null) { | |
mListener.onProgressUpdate(progress); | |
} | |
} | |
@Override | |
protected void onPostExecute(File result) { | |
if (mThrowable != null) { | |
if (mListener != null) { | |
mListener.onCatchException(mThrowable); | |
} | |
return; | |
} | |
if (isCancelled()) { | |
return; | |
} | |
if (result != null && mListener != null) { | |
mListener.onPostExecute(result); | |
} | |
mIsNowDownloding = false; | |
result = null; | |
} | |
@Override | |
protected void onCancelled(File result) { | |
if (mIsNowDownloding && (result != null) && result.exists()) { | |
result.delete(); | |
} | |
if (mListener != null) { | |
mListener.onCancelled(result); | |
} | |
mIsNowDownloding = false; | |
result = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment