How to download file to android
How to download file to android
How to download files from the Web in the Android Browser?
After trying for a long while to figure it out, I still cannot convince my Android browser to save the file from a web link. If the link is a picture, holding over it and pressing «Save As» saves the picture, not the file it links to. My Android device has no multitouch (is this required to tell to browser to download?), and simply clicking on a download link takes me nowhere. Can anyone help me?
4 Answers 4
Nowadays the built-in Android browser has been replaced by Chrome, which does this perfectly well. You can long-press and save virtually any image or link, and even videos from a number of sites that couldn’t be easily done on the desktop.
Answer below for older devices:
The Android browser by default can only download files that the Android system «recognizes» (i.e., there has to be a program registered to handle that file type). It’s a stupid restriction in my opinion, but you can install Download Crutch to overcome this limitation (it registers itself for every filetype).
If you’re referring to images that link to something else and you want to save the something else, long-press on the image and choose «Save Link As» or similar.
Personally I usually just click the link of the file and it downloads.
As for the image: try long pressing but instead of choosing «Save As» look for «Copy link URL» or «Open in New Window» or something similar to those two menu options. «Copy link URL» should copy the URL of the file to your clipboard, then you can paste that link in the address bar and hit «Go» and it should download your file. «Open in New Window» should essentially do the same thing (i’m just trying to give you as many options as I can to try). If you click on the link it may not take you anywhere but to a blank page while the file downloads.
So to check your download list: You may have already tried this but to see if the file was downloaded you can open your browser, open the menu, then select «More», you should see «Downloads». This is where your downloaded files list is.
Another way you could check is to look in the «downloads» folder on your sdcard (/sdcard/downloads/) using ES File Explorer or some other file explorer.
If this is not a solution let me know and give an example of what you are trying to accomplish and I’ll try to figure it out.
Retrofit 2 — How to Download Files from Server
Moonshoot
Moonshoot is a
Student Feature.
If this is your first Retrofit post on futurestud.io, feel free to browse our other Retrofit posts:
Retrofit Series Overview
Getting Started and Creating an Android Client
Basics of API Description
Creating a Sustainable Android Client
URL Handling, Resolution and Parsing
How to Change API Base Url at Runtime
Multiple Server Environments (Develop, Staging, Production)
Share OkHttp Client and Converters between Retrofit Instances
Upgrade Guide from 1.9
Beyond Android: Retrofit for Java Projects
How to use OkHttp 3 with Retrofit 1
Synchronous and Asynchronous Requests
Send Objects in Request Body
Add Custom Request Header
Manage Request Headers in OkHttp Interceptor
Dynamic Request Headers with @HeaderMap
Multiple Query Parameters of Same Name
Optional Query Parameters
Send Data Form-Urlencoded
Send Data Form-Urlencoded Using FieldMap
How to Add Query Parameters to Every Request
Add Multiple Query Parameter With QueryMap
How to Use Dynamic Urls for Requests
Constant, Default and Logic Values for POST and PUT Requests
Reuse and Analyze Requests
Optional Path Parameters
How to Send Plain Text Request Body
Customize Network Timeouts
How to Trust Unsafe SSL certificates (Self-signed, Expired)
Dynamic Endpoint-Dependent Interceptor Actions
How to Update Objects on the Server (PUT vs. PATCH)
How to Delete Objects on the Server
Ignore Response Payload with Call
Receive Plain-String Responses
Crawl HTML Responses with jspoon (Wikipedia Example)
Loading Data into RecyclerView and CardView
Introduction to (Multiple) Converters
Adding & Customizing the Gson Converter
Implementing Custom Converters
How to Integrate XML Converter
Access Mapped Objects and Raw Response Payload
Supporting JSON and XML Responses Concurrently
Handling of Empty Server Responses with Custom Converter
Send JSON Requests and Receive XML Responses (or vice versa)
Unwrapping Envelope Responses with Custom Converter
Wrapping Requests in Envelope with Custom Converter
Define a Custom Response Converter
Simple Error Handling
Error Handling for Synchronous Requests
Catch Server Errors Globally with Response Interceptor
How to Detect Network and Conversion Errors in onFailure
Log Requests and Responses
Enable Logging for Development Builds Only
Log Network Traffic with Stetho and Chrome Developer Tools
Using the Log Level to Debug Requests
Analyze Network Traffic with Android Studio Profiler
Debug and Compare Requests with RequestBin
Introduction to Call Adapters
Custom Call Adapter to Separate OnResponse Callback
How to Integrate RxJava 1.x Call Adapter
How to Integrate RxJava 2.x Call Adapter
How to Integrate Guava Call Adapter
Custom Call Adapter to Separate Network and Gson Errors
Pagination Using Query Parameter
Pagination Using Link Header and Dynamic Urls (Like GitHub)
Pagination Using Range Header Fields (Like Heroku)
How to Upload Files to Server
How to Upload Multiple Files to Server
How to Upload a Dynamic Amount of Files to Server
Upload Files with Progress
Passing Multiple Parts Along a File with @PartMap
How to Download Files from Server
Download Files with Progress Updates
How to Upload Files to Server
Basic Authentication on Android
Token Authentication on Android
OAuth on Android
Hawk Authentication on Android
How to Refresh an Access Token
Activate Response Caching (Etag, Last-Modified)
Check Response Origin (Network, Cache, or Both)
Force Server Cache Support with Response Interceptor
Support App Offline Mode by Accessing Response Caches
Analyze Cache Files
Basics of Mocking Server Responses
Customizing Network Behavior of Mocked Server Responses
Mock APIs with JsonServer
Fluent Interface with Builders
How to Specify the Retrofit Request
If you’re reading this and you haven’t written code for any Retrofit requests yet, please check our previous blog posts to get started. For all you Retrofit experts: the request declaration for downloading files looks almost like any other request:
If the file you want to download is a static resource (always at the same spot on the server) and on the server your base URL refers to, you can use option 1. As you can see, it looks like a regular Retrofit 2 request declaration. Please note, that we’re specifying ResponseBody as return type. You should not use anything else here, otherwise Retrofit will try to parse and convert it, which doesn’t make sense when you’re downloading a file.
The second option is new to Retrofit 2. You can now easily pass a dynamic value as full URL to the request call. This can be especially helpful when downloading files, which are dependent of a parameter, user or time. You can build the URL during runtime and request the exact file without any hacks. If you haven’t worked with dynamic URLs yet, feel free to head over to our blog post for that topic: dynamic urls in Retrofit 2
Pick what kind of option is useful to you and move on to the next section.
How to Call the Request
After declaring our request, we need to actually call it:
There is just one thing left, which currently hides behind the function writeResponseBodyToDisk() : writing the file to the disk!
How to Save the File
The writeResponseBodyToDisk() method takes the ResponseBody object and reads and writes the byte values of it to the disk. The code looks much more difficult than it actually is:
Most of it is just regular Java I/O boilerplate. You might need to adjust the first line on where and with what name your file is being saved. When you have done that, you’re ready to download files with Retrofit!
But we’re not completely ready for all files yet. There is one major issue: by default, Retrofit puts the entire server response into memory before processing the result. This works fine for some JSON or XML responses, but large files can easily cause Out-of-Memory-Errors.
If your app needs to download even slightly larger files, we strongly recommend reading the next section.
Beware with Large Files: Use @Streaming!
If you’re downloading a large file, Retrofit would try to move the entire file into memory. In order to avoid that, we’ve to add a special annotation to the request declaration:
Thus, the final step is to wrap the call into a separate thread, for example with a lovely ASyncTask:
You can still use the same writeResponseBodyToDisk() method. If you remember the @Streaming declaration and this snippet, you can download even large files with Retrofit efficiently.
Next: Download Files with Progress Updates
If you download files in the foreground and they are not small, you might want to inform the user on your actions. Ideally, you would display progress updates how much you’ve downloaded already. We’ve another tutorial on how to download files with progress updates.
Summary
In this tutorial you’ve seen how to download files with Retrofit efficiently. This is everything you need to know to download files with Retrofit.
If you’ve any questions, let us know in the comments or on twitter.
Still Have Questions? Get Our Retrofit Book!
All modern Android apps need to do network requests. Retrofit offers you an extremely convenient way of creating and managing network requests. From asynchronous execution on a background thread, to automatic conversion of server responses to Java objects, Retrofit does almost everything for you. Once you’ve a deep understanding of Retrofit, writing complex requests (e.g., OAuth authentication) will be done in a few minutes.
Invest time to fully understand Retrofit’s principles. It’ll pay off multiple times in the future! Our book offers you a fast and easy way to get a full overview over Retrofit. You’ll learn how to create effective REST clients on Android in every detail.
Boost your productivity and enjoy working with complex APIs.
Get Notified on New Future Studio
Content and Platform Updates
Get your weekly push notification about new and trending
Future Studio content and recent platform enhancements