Showing posts with label Downloading Image in Android. Show all posts
Showing posts with label Downloading Image in Android. Show all posts

Sunday, 22 May 2016

Downloading Image through URL in Android

public void downloadFile(String uRl) {
String [] urlTemp = uRl.split("/");
String photoName = urlTemp[urlTemp.length-1];
File direct = new File(Environment.getExternalStorageDirectory()
+ "/TempFolderName/"+photoName);
// I had added this line so that no duplication of file should be happen.
if (!direct.exists()) {
File direct1 = new File(Environment.getExternalStorageDirectory()
+ "/TempFolderName");
if (!direct1.exists()) {
direct1.mkdir();
}
DownloadManager mgr = (DownloadManager) getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/TempFolderName", photoName);
mgr.enqueue(request);
}

}