Download PDF From Any URL in Android Studio – Solution

Are you looking for a way to download PDF files from any URL in Android Studio programmatically? In this comprehensive guide, we’ll show you how to download PDF files using two different methods, one using the view intent and the other using the download manager within the app we’re building.

Before we get started, we recommend checking out this YouTube video that shows you how to download PDF files in Android Studio programmatically. It’s a great resource for those who prefer a visual walkthrough.

Downloading PDF file using Default Browser [View Intent]

The first method we’ll cover is downloading PDF files using the view intent. This method is useful when you want the browser to handle the download process.

Create an Activity to Download PDF File

The first step is to create an activity in Android Studio. We’ll call it “DownloadPDFActivity.” We’re assuming you’ve created both Layout and Java file. In the Layout file you can add a simple button that will download pdf file on click, and a simple editText that will have the url. In you real app, this url can be from any resource desired:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_download_pdf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Download PDF File" />

    <Edittext
        android:id="@id/btn_url"
        android:text="URL"/>

</LinearLayout>

Add Required Permissions to Manifest

Next, we need to add permissions to our AndroidManifest.xml file. We’ll add the INTERNET permission to allow our app to connect to the internet.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pdfdownloadapp">

    <!-- Network permission -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- Permission to write to external storage -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        ...
    </application>

</manifest>

Implement the View Intent

In our activity’s onCreate method, we will add reference to the button and edittext that we have in our layout. We will create a function that will download the PDF file using the default browser. In the function, we’ll create an intent to view the PDF file using the VIEW action. We’ll pass the URL of the PDF file as a parameter to the intent.. In the button’s onClick, we can call the function and our PDF file will be downloaded. Here’s what the function looks like.

private void Download_PDF_View_Intent(String url) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    startActivity(intent);
}

Run the App to Download PDF using Browser

Finally, we’ll run the app and click on the “Download PDF” button. The browser will handle the download process and save the PDF file to the user’s preferred location.

Downloading PDF file using DownloadManager

The second method we’ll cover is downloading PDF files using the Download Manager. This method is useful when you want to handle the download process within your app.

Create an Activity to Download PDF File

As before, let’s create an activity in Android Studio. We’ll call it “DownloadPDFActivity2.” The XML layout for the activity can be same as before, or in fact you can use the same layout. The only change we will be doing is instead of passing the URL as a parameter in an intent, we will use downloadManager to download the file from within our app.

Add Write Permissions to Manifest

Next, we need to add permissions to our AndroidManifest.xml file. In addition to the INTERNET permission, we’ll also need the WRITE_EXTERNAL_STORAGE permission to allow our app to save the PDF file to the user’s device. Here’s what the manifest file permissions look like:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pdfdownloadapp">

    <!-- Network permission -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- Permission to write to external storage -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        ...
    </application>

</manifest>

Implement the Download Manager

In our activity’s onCreate method, we’ll create an instance of the Download Manager and initiate the download process. We’ll specify the URL of the PDF file, the title of the download, and the destination path on the user’s device.

private void Download_PDF_Internal_Storage(String url) {
    Download_Uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(Download_Uri);

    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setAllowedOverRoaming(false);
    request.setTitle("PDF File Download");
    request.setDescription("Downloading " + "pdf" + " file using Download Manager.");
    request.setVisibleInDownloadsUi(true);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "pdf_name.pdf");
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

    refid = downloadManager.enqueue(request);

    Toast.makeText(this, "PDF Download Started", Toast.LENGTH_SHORT).show();

    BroadcastReceiver onComplete = new BroadcastReceiver() {
        public void onReceive(Context ctxt, Intent intent) {
            // Do something on download complete
        }
    };

    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

You can use this function on the download button’s onclick and the pdf file will be downloaded and saved to internal storage.

Handle Download Completion

As you can see above, we’ve registered a receiver that will be notified when the download is complete. In the onReceive method of the broadcastReceiver, you can do necessasry steps after the completion of the download.

Conclusion

In conclusion, downloading PDF files from any URL in Android Studio is a simple process that can be done using either the view intent or the download manager. Both methods are effective, and the choice depends on your app’s requirements. With the knowledge and tools provided in this guide, you’ll be able to download PDF files programmatically and save them to internal storage in no time. If you have any issues, feel free to comment below. Cheers!

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

Sharing is caring!

Leave a Comment

Your email address will not be published.