Skip to content

Instantly share code, notes, and snippets.

@PasanBhanu
Created September 26, 2019 09:04
Show Gist options
  • Save PasanBhanu/46f08845ee3b7acf93de81acf8048fa2 to your computer and use it in GitHub Desktop.
Save PasanBhanu/46f08845ee3b7acf93de81acf8048fa2 to your computer and use it in GitHub Desktop.
Download Image from URL and Added to Gallery - Android (Java)
/*
This method can be used to download an image from the internet using a url in Android. This use Android Download Manager to
download the file and added it to the Gallery. Downloaded image will be saved to "Pictures"
Folder in your internal storage
*/
private void downloadImageNew(String filename, String downloadUrlOfImage){
try{
DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(downloadUrlOfImage);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(filename)
.setMimeType("image/jpeg") // Your file type. You can use this code to download other file types also.
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,File.separator + filename + ".jpg");
dm.enqueue(request);
Toast.makeText(this, "Image download started.", Toast.LENGTH_SHORT).show();
}catch (Exception e){
Toast.makeText(this, "Image download failed.", Toast.LENGTH_SHORT).show();
}
}
@7afidi
Copy link

7afidi commented Jan 10, 2021

Thanks!

@laithalaboodi
Copy link

Great work!

@ismailrisky01
Copy link

Thanks, it work

@pyaephyo4325
Copy link

THANK YOU !

@yash292005
Copy link

great work bro thanks

@tdkhoasg
Copy link

Amazing, Thanks u!

@mahmood199
Copy link

Does the downloaded image show in phone gallery?

@PasanBhanu
Copy link
Author

@mahmood199 yes it is showing

@sumitsharma121
Copy link

sumitsharma121 commented Sep 21, 2021

super working this code thank you so mach

@jonrysimbolon
Copy link

nice !!!

@dev-bahodir
Copy link

great thanks

@rkhang7
Copy link

rkhang7 commented Dec 1, 2021

thanks a lot

@DerGoogler
Copy link

Works on android 11

@vstandroidbk
Copy link

This worked thank you

@Sumit2499
Copy link

Sumit2499 commented Feb 1, 2022

Bro this is the easiest way Thanks Bro
2 feb 2022 Android 11 working

@fekri8614
Copy link

Very good!! Thank you for sharing!

@vinitfreakk
Copy link

one of the easiest way I have ever seen .💫💫

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment