I want to download a pdf file from an url. For viewing the pdf file I used the code below.
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
It is working but how do I get the pdf file from an url (e.g http://.../example.pdf
). I want to download the pdf file from this url. Please help me. Thanks in advance.
Downloading a PDF works the same as downloading any other binary file.
HttpUrlConnection
getInputStream()
method to read the file.FileOutputStream
and write the inputstream.Check this post for example source code.
For more info click here http://androiddhina.blogspot.in/2015/09/how-to-download-pdf-from-url-in-android.html
There is no need to put lengthy code to open and download pdf in android you can just use below code to open and download pdf
Download a pdf:
Ah as I've just found out this is device dependent.
scenarios
It will download the pdf to your browser/downloaded/ folder
You have a google docs account - It will ask you to sign in then view the pdf in the browser
You have a pdf reader installed - App dependent may catch it may not
In all scenarios though the user has access to the PDF with one line of code :-)
There are many ways to download files. Following I will post most common ways; it is up to you to decide which method is better for your app.
1. Use
AsyncTask
and show the download progress in a dialogThis method will allow you to execute some background processes and update the UI at the same time (in this case, we'll update a progress bar).
This is an example code:
The
AsyncTask
will look like this:The method above (
doInBackground
) runs always on a background thread. You shouldn't do any UI tasks there. On the other hand, theonProgressUpdate
andonPreExecute
run on the UI thread, so there you can change the progress bar:For this to run, you need the WAKE_LOCK permission.
2. Download from Service
The big question here is: how do I update my activity from a service?. In the next example we are going to use two classes you may not be aware of:
ResultReceiver
andIntentService
.ResultReceiver
is the one that will allow us to update our thread from a service;IntentService
is a subclass ofService
which spawns a thread to do background work from there (you should know that aService
runs actually in the same thread of your app; when you extendsService
, you must manually spawn new threads to run CPU blocking operations).Download service can look like this:
Add the service to your manifest:
And the activity will look like this:
Here is were
ResultReceiver
comes to play:2.1 Use Groundy library
Groundy is a library that basically helps you run pieces of code in a background service, and it is based on the
ResultReceiver
concept shown above. This library is deprecated at the moment. This is how the whole code would look like:The activity where you are showing the dialog...
A
GroundyTask
implementation used by Groundy to download the file and show the progress:And just add this to the manifest:
It couldn't be easier I think. Just grab the latest jar from Github and you are ready to go. Keep in mind that Groundy's main purpose is to make calls to external REST apis in a background service and post results to the UI with easily. If you are doing something like that in your app, it could be really useful.
2.2 Use https://github.com/koush/ion
3. Use
DownloadManager
class (GingerBread
and newer only)GingerBread brought a new feature,
DownloadManager
, which allows you to download files easily and delegate the hard work of handling threads, streams, etc. to the system.First, let's see a utility method:
Method's name explains it all. Once you are sure
DownloadManager
is available, you can do something like this:Download progress will be showing in the notification bar.
Final thoughts
First and second methods are just the tip of the iceberg. There are lots of things you have to keep in mind if you want your app to be robust. Here is a brief list:
INTERNET
andWRITE_EXTERNAL_STORAGE
); alsoACCESS_NETWORK_STATE
if you want to check internet availability.Unless you need detailed control of the download process, then consider using
DownloadManager
(3) because it already handles most of the items listed above.But also consider that your needs may change. For example,
DownloadManager
does no response caching. It will blindly download the same big file multiple times. There's no easy way to fix it after the fact. Where if you start with a basicHttpURLConnection
(1, 2), then all you need is to add anHttpResponseCache
. So the initial effort of learning the basic, standard tools can be a good investment.