How to Load PDF from URL in Android?
Most of the apps require to include support to display PDF files in their app. So if we have to use multiple PDF files in our app it is practically not possible to add each PDF file inside our app because this approach may lead to an increase in the size of the app and no user would like to download the app with such a huge size. So to tackle this issue related to the size we will load PDF files from the server directly inside our app without actually saving that files inside our app. Loading PDF files from the server will help us to manage the increase in the size of our app. So in this article, we will take a look at How to Load PDF files from URLs inside our Android App.
Implementation of PDFView
For adding this PDF View we are using a library that will help us to load PDF from URL. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add dependency to build.gradle(Module:app)
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
Now sync option will appear at the top right corner click on the sync now option.
Step 3: Add permission to the internet in your AndroidManifest.xml file
Add below two lines inside your AndroidManifest.xml file.
<!–Permission for internet–>
<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 5: Working with the MainActivity.java file
Navigate to the app > java > your apps package name > MainActivity.java file. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Note: If you have updated to Android Studio to 4.0 or onwards then you may face the following errorExecution failed for task ':app:stripDebugDebugSymbols'. NDK at ~/Library/Android/sdk/ndk-bundle did not have a source.properties file
Please refer to this to fix the error.
Check out the project: https://github.com/ChaitanyaMunje/GFGImageSlider/tree/PDFViewer