Skip to content

Instantly share code, notes, and snippets.

@carlosefonseca
Last active December 15, 2015 23:59
Show Gist options
  • Save carlosefonseca/5344540 to your computer and use it in GitHub Desktop.
Save carlosefonseca/5344540 to your computer and use it in GitHub Desktop.
Dead simple async downloader of URLs to file or string, with support for shortened links, a progressdialog and a completion handler.
public static class DownloadResult {
public void onFile(String path) {};
public void onString(String string){};
public void onFail() {};
}
/**
* This class handles the download of stuff from the tubes.
* It's an extension of AsyncTask, so it will download asynchronously.
* You can download to a file, which will be downloaded to the {@link android.os.Environment#getExternalStorageDirectory()} folder,
* or to a string.
* The path to the file or the string will be returned to your handler.
* You can pass in a ProgressDialog to be updated during the download.
* You can pass in a shortened URL, it will expand it. Only tested with http://tiny.cc URLs.
*/
public static class DownloadURL extends AsyncTask<String, Integer, String> {
ProgressDialog progressDialog = null;
boolean isFile = false;
private final DownloadResult handler;
public DownloadURL(ProgressDialog pd, boolean isFile, DownloadResult handler) {
progressDialog = pd;
this.isFile = isFile;
this.handler = handler;
}
@Override
protected String doInBackground(String... sUrl) {
try {
URL possibilyShortenedURL = new URL(sUrl[0]);
URL url;
URLConnection connection;
HttpURLConnection ucon = (HttpURLConnection) possibilyShortenedURL.openConnection();
if (ucon.getResponseCode() != 200) {
// yep, shortened
ucon.setInstanceFollowRedirects(false);
url = new URL(ucon.getHeaderField("Location"));
connection = url.openConnection();
} else {
// real url
connection = ucon;
url = possibilyShortenedURL;
}
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
Log.v(TAG, "File length: " + fileLength);
if (fileLength == -1) {
return null;
}
if (progressDialog != null)
progressDialog.setMax(fileLength / 1024);
if (isFile) {
// download the file
InputStream input = new BufferedInputStream(url.openStream());
long total = 0;
int count;
byte data[] = new byte[1024];
File file = new File(Environment.getExternalStorageDirectory().getPath(), new File(url.getPath()).getName());
OutputStream output = new FileOutputStream(file);
while ((count = input.read(data)) != -1) {
if (isCancelled()) break;
total += count;
// publishing the progress....
publishProgress((int) total / 1024);// (int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return file.getAbsolutePath();
} else {
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String fullText = "";
String inputLine;
while ((inputLine = in.readLine()) != null)
fullText += inputLine;
in.close();
return fullText;
}
} catch (Exception e) {
Log.e(TAG, "Download of " + sUrl[0] + " failed", e);
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (progressDialog != null)
progressDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
if (progressDialog != null)
progressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String s) {
if (s == null)
handler.onFail();
if (isFile)
handler.onFile(s);
else
handler.onString(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment