Friday, September 20, 2013

Downloading a file remotely hosted on HTTP for Android in Java

This script downloads a file from a remote http location, and places the contents of the file on your device (external or internal directory downloads). The file will appear in your notifications and actually utilize the built-in download manager (as long as you have ANDROID HONEYCOMB BUILD).

public void downloadFile() {
Log.v(TAG, "Downloading File...");
String url = "http://www.apolloss.com/myFile.zip";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("This is the Zip file to download");
request.setTitle("update zip file");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myFile.zip");

// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

}

No comments:

Post a Comment