Android Download a File from Internet URL in Java

How to Download a File from URL Link in Android Java Tutorial

This tutorial will guide you to learn how to download a file from web in android java, we will first get a link to an image for downloading it, add required permissions and then pass the download link to the download manager for downloading, we will also set a path for saving the file to a location in the android device.



First, Create a new android project in Android Studio or use an existing project.

Add these permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Next, we need an image to test our download code, let’s try this cat’s image that is hosted in this website.

This is the direct link of the cat’s image



Open the, Then add this code, this code should be placed below OnCreate method.

try {
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES), "myfolder");

if (!imageStorageDir.exists()) {
//noinspection ResultOfMethodCallIgnored

// default image extension
String imgExtension = ".jpg";

if (image_uri.toString().contains(".gif"))
imgExtension = ".gif";
else if (image_uri.toString().contains(".png"))
imgExtension = ".png";
else if (image_uri.toString().contains(".3gp"))
imgExtension = ".3gp";

String date = DateFormat.getDateTimeInstance().format(new Date());
String file = getString(R.string.app_name) + "-image-" + date.replace(" ", "").replace(":", "").replace(".", "") + imgExtension;

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
              Uri downloadUri = Uri.parse("");
              DownloadManager.Request request = new DownloadManager.Request(downloadUri);

              request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                      .setDestinationInExternalPublicDir(DIRECTORY_PICTURES + File.separator, file)


              Toast.makeText(getApplicationContext(), getString(R.string.downloading_img), Toast.LENGTH_LONG).show();

          } catch (IllegalStateException ex) {
              Toast.makeText(getApplicationContext(),"Storage Error", Toast.LENGTH_LONG).show();
          } catch (Exception ex) {
              // just in case, it should never be called anyway
              Toast.makeText(getApplicationContext(),"Unable to save image", Toast.LENGTH_LONG).show();




So this is the code for downloading image from a Web link URL to your android device’s pictures directory, the code will be executed as soon as you start the app or the MainActivity.

Read:  How to Forget Someone You Love so Much



  • This code will not cause exceptions because exceptions are caught, so your app will not crash when executing this code
  • If your android device runs on marshmallow, you will need request permission before executing this code.
  • This code is just for test, you can assign this code to a button or to your desired activity.
  • You can try other direct download links to test this code

Update: If you are new to android development, or just want fix this error, i can fix it for you for a small fee of US $2 (₹120 indian rupees). Just send me your project or that java file (The Activity where you wish to add this code) through email or google drive, i will fix your project or file and send it back to you. You can pay me through Paypal, Direct Bank Transfer (NEFT,IMPS,UPI) etc. i do this service to all countries. For more details, contact me through [email protected] or [email protected]

, , ,

Post navigation

One thought on “Android Download a File from Internet URL in Java


This site uses Akismet to reduce spam. Learn how your comment data is processed.