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. If you can’t do it, you can contact me, i will fix or add it for you (see below for contact details)
First, Create a new android project in Android Studio or use an existing project.
Add these permissions in AndroidManifest.xml
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
http://www.zidsworld.com/wp-content/uploads/2018/06/cat_1530281469.jpg
Open the MainActivity.java, Then add this code, this code should be placed below OnCreate method.
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.
Conclusion
- 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 $5. Just send me your project or that java/kotlin file (The Activity where you wish to add this code) through email or google drive, i can also do it online through TeamViewer, AnyDesk etc. I will fix your project or file and send it back to you. You can pay me through Paypal, PayTM, GooglePay, PhonePe, UPI etc. For more details, contact me below
Contact Now
- WhatsApp: SendWhatsAppMessage (+91 963303 9471)
- Call: Clicktocall
- Telegram: TelegramMessage
- Email: [email protected]
Meet Advanced Plus
I highly recommend you use the new and advanced Android Webview Advanced Plus Source Code we developed to easily convert any website to android app. No coding required, just set your website link, app color, icon etc and the app will be ready!, and it supports upload, download (blob pdf download also supported), loading progress bars, notification, night mode etc. To learn more about the Android Advanced Webview Source Code and to download it, head over to this page Download Android Webview Source Code
Источник
Downloading file from url android
Android Download Files & Save
In this tutorial we are going to learn how to download pdf, doc , video, mp3, zip ,etc. files from server and save them in device memory.
Below are certain things that we are going to learn in this tutorial :
1. Download Files – Download Files from Server Url.
2. Save Files – Create Folder in device and save the dowloaded files.
3. Download Multiple Files – Download Many Files at a time.
VIDEO DEMO
1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.
2. Open res=>values=strings.xml and add the following strings to it.
3. Create a xml layout naming activity_main.xml and add the following code. In this layout there are some Buttons for showing downloading different type of files.
4. Create a Utils.java class and write all download urls here.
5. Create a CheckForSDCard.java class for checking if SDCard is present or not.
6. Now create a DownloadTask.java and add the following code. In this code AsyncTask is used for downloading data from server. There are several main steps in doInBackground(Void…arg0) that are listed below :
1. Converting download url into URL:
2. Opening Url Connection for data downloading:
3. Setting request method to server:
4. Connect the open connection:
5. Create a directory in SD Card if not present:
6. Create an Output file i.e. download file inside above created directory:
Note: Give extension to your outputFile like .pdf, .mp3, .mp4 etc.
7. Download File and write it over the outputFile created:
8. Close all connection after downloading:
Full Code of DownloadTask.java
7. Now create MainActivity.java and add the following code. In this code i am starting AsyncTask on button click.
But before downloading it is necessary there should be a proper internet connection. So for this I had created a method naming isInternetConnected() that will return true or false.
If you are new in how to detect internet connection then check Detect Internet Connection Tutorial.
Last Button is for Opening downloaded folder and the code for opening download folder is below:
Источник