Skip to content

Instantly share code, notes, and snippets.

@Folyd
Created September 15, 2015 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Folyd/bc0f909594a3c91588f9 to your computer and use it in GitHub Desktop.
Save Folyd/bc0f909594a3c91588f9 to your computer and use it in GitHub Desktop.
Abstract Download AsyncTask
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public abstract class AbstractDownloadAsyncTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
String path = "";
if (params == null) {
return path;
}
File file = getDestinationFile();
if (file.exists()) {
int i = 0;
while (i <= 100) {
super.publishProgress(i++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
path = file.getAbsolutePath();
android.util.Log.i("Download", "is downloaded " + " size:" + file.length());
return path;
}
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
String downloadUrl = params[0];
URL url = new URL(downloadUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setDoInput(true);
connection.connect();
// Expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file.
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
android.util.Log.i("Download", "Server returned HTTP " +
connection.getResponseCode() + " " + connection.getResponseMessage());
path = "";
return path;
}
// Download the file
input = connection.getInputStream();
output = new FileOutputStream(file);
// This will be useful to display download percentage
// might be -1: server did not report the length
final int fileLength = connection.getContentLength();
byte data[] = new byte[1024];
long total = 0;
int len;
while ((len = input.read(data)) != -1) {
total += len;
// Publishing the progress.
// Only if total length is known.
if (fileLength > 0) {
super.publishProgress((int) (total * 100 / fileLength));
}
output.write(data, 0, len);
}
//Check whether the task been canceled.
if (!isCancelled()) {
//Assign the value to path if download completely success.
path = file.getAbsolutePath();
}
} catch (IOException e) {
e.printStackTrace();
path = "";
} finally {
try {
if (output != null) output.close();
if (input != null) input.close();
} catch (IOException ignored) {
}
if (connection != null) connection.disconnect();
}
return path;
}
@Override
protected void onCancelled(String path) {
super.onCancelled(path);
File file = getDestinationFile();
Log.d("AbstractDownload", "path when cancel--->" + path);
Log.d("AbstractDownload", "destination file--->" + file);
Log.d("AbstractDownload", "destination file exist--->" + file.exists());
//Delete the previous existed and incomplete file if download canceled by user.
if (TextUtils.isEmpty(path) && file.exists()) {
file.delete();
}
}
protected abstract File getDestinationFile();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment