Giving a user the ability to download files in your app can be difficult to figure out. In iOS, you can use AlamoFire to download the file locally and then present it with a UIDocumentInteractionController. (The code would look something like this.) It presents the documents, images, gifs, videos, etc. in the app for you, and you can then download to the device from there. Show Unfortunately, this isn’t simple in Android because there are many OEMs for Android devices. (In-app image viewing can be handled using Glide. If you absolutely need to view PDFs in-app, you can probably find some solution, but I would recommend just giving the users download ability and letting their device handle it.) Today, I’ll explain how to download files in Kotlin using Ktor and intents. Initial SetupThe first thing you will need are some dependencies, Ktor, and coroutines. Add these to your app Gradle. Ktor allows for asynchronous communication, which is very useful for file downloading and reporting file progress.
Then we need to add this file into res.xml. (You’ll likely need to create the xml resources folder.) It adds an external path.
In the AndroidManifest, make sure to add these permissions:
Add the provider. This uses the external path we defined above for the FileProvider.
Downloader CoroutineThe coroutine for downloading files will be an extension on Ktor’s HttpClient. First, we need a class to return during the coroutine to report on the status of the download.
This extension creates a coroutine that takes an output stream and URL. While the file is read in, the current progress is emitted. Once finished, the data is written to the output stream, and success is returned. Otherwise, there was some failure.
This code was originally found on Kotlin Academy. The ViewModel and LayoutBefore making the fragment, we need a view model and a layout. The view model is simple, only containing a Boolean to indicate if the download is occurring.
The layout itself is also simple, just a button (which is enabled when the file isn’t downloading) and a horizontal progress bar. Note that material-style circle progress bars can’t have progress set and continually spin; they’re more useful for other types of requests.
The FragmentThe base of the fragment is straightforward enough. Of note are the permissions and codes: if the app doesn’t have these permissions, the app can’t download files. The codes don’t need to be “1” and “2,” but if there are others in the app, they should all be unique. When the fragment loads, if we have permissions, we can set the button click listener; otherwise, we have to request permission. Some apps request these permissions on first load if they require them throughout the app. Others only request them where they are actually needed. If your app has a specific place where file downloads occur, you can just request permission there.
First, we need functions to check the permissions and the request result. Note that only devices with Marshmallow (API 23) or later need permissions; earlier devices had them by default.
If permission needs to be requested, our return should set the click listener when permission is granted. Either you can force the download to occur where you want with the name you provide, or you can let the user decide the name and location. I use the latter case here; the click will start an intent for creating a document with some defaults set. 0The result will use the returned URI as the location for downloading the file. 1Downloading the file opens the output stream to the URI given and dispatches the download file coroutine. The download itself is handled on the IO thread, but the emitter results are handled on the Main thread. This allows for the correct asynchronous updates (in this case, updating the progress bar). 2The viewFile function takes the same URI given and opens it in an intent, if able. If there are multiple applications where the file can be viewed, it presents a chooser. How do I download a URL file?Go to the webpage where you want to download the file. Save the file: Most files: Click on the download link. Or, right-click on the file and choose Save as.. Windows 10, 8, 7 and Vista: \Users\<username>\Downloads.. Mac: /Users/<username>/Downloads.. Linux: /home/<username>/Downloads.. How do I download a URL on Android?Go to the webpage where you want to download a file. Touch and hold what you want to download, then tap Download link or Download image. To see all the files you've downloaded to your device, open the Downloads app.
How to download file from URL in Java?We can use java. net. URL openStream() method to download file from URL in java program. We can use Java NIO Channels or Java IO InputStream to read data from the URL open stream and then save it to file.
How do I download a file directly from URL in spring boot?How To Download A File Directly From URL In Spring Boot. Download File Using StreamingResponseBody. StreamingResponseBody is a functional interface. ... . Download File using InputStream to HttpServletResponse. To read files in java we can use Reader or Stream. ... . Download File using InputStreamResource.. |