- How to open a PDF file in Android programmatically?
- Project setup
- Ways of opening PDF in Android
- Making UI for the project
- Opening a PDF file in Android using WebView
- Opening a PDF file in Android using AndroidPdfViewer library
- Assets Folder
- From Phone Storage
- PDF from Internet
- Project source code and What next?
- 20+ Best Apps To Open All Type Of File Formats In Android
- List of Best Apps To Open All type of Files
- 1. Open Video files
- 2. Open All Type Of Compressed Files (Zip, RAR, etc.)
- 3. Open All Type Of Document Files (PDF, DOCX, TXT, and all others)
- 4. Open All Type Of Audio Format (3GP, MP3, MP4, M4A, AAC, etc.)
- 5. Open Image Files Of Any Format( JPEG, PNG, JPG, BMP, GIF)
How to open a PDF file in Android programmatically?
Opening and viewing documents in Android applications are very interesting and a must to have in every application. You can open any application present on your mobile and you will find that every application contains documents in some or the other way. And among these documents, the most popular and widely used document format is the PDF format.
PDF or Portable Document Format is a file format that has captured all the elements of a printed document. This is the most used document format. For example, in the Paytm application, you get your monthly expenses in the form of PDF document. So, if you also want to display some kind of document in your application, then you can open this PDF format document.
So, welcome to MindOrks and in this tutorial, we will learn how to open a PDF file in Android programmatically. We will cover the below topics in this tutorial:
- Project setup
- Ways of opening PDF in Android
- Making UI and adding Activities for the project
- Opening a PDF file using WebView
- Opening a PDF file using AndoirdPdfviewer library
- Project source code and What next?
Project setup
In this tutorial, we will make a project and try various ways of opening PDF file, Here we are going to set up our project:
- Start a new Android Studio Project
- Select Empty Activity and Next
- Name: Open-PDF-File-Android-Example
- Package name: com.mindorks.example.openpdffile
- Language: Kotlin
- Finish
- Your starting project is ready now
- Under your root directory, create a package named utils .(right-click on root directory > new > package)
- In the utils package, create one object classes: FileUtils .(right-click on utils > new > Kotlin file/class > Object class)
If you are preparing for your next Android Interview, Join our Android Professional Course to learn the latest in Android and land job at top tech companies.
Ways of opening PDF in Android
If you want to display PDF in your Android application, there are various ways of doing it. Some of the ways of opening the PDF can be:
- From Assets: Let’s take an example, if you want to display some icons in your application then you will put all your icons in the drawable folder and then you will use those icons in your application. Same is with the case of PDF files also. If you have some PDF file that is constant and you want to display it in your application then you can put that PDF file in the assets folder and use that PDF in your app. One example can be the Terms and Conditions file. The terms and conditions files are rarely changed. So, you can put that document in the assets folder and use it.
- From Device: The other way of opening a PDF is to open it from the device itself. Here, you can open the PDF files present in your mobile device. This is the most used approach for opening the PDF in an Android device.
- From the Internet: Here, you can open PDF files from the internet. All you need to do is just use the URL of the PDF file and after downloading the PDF file, you can open the PDF file in your mobile application.
So, we will look upon all these ways of viewing the PDF in your Android Application. Let’s make the UI of the project.
Making UI for the project
In our example, we are going to cover four different cases:
- Opening a PDF file using WebView
- Opening a PDF file from assets using AndroidPdfViewer library
- Opening a PDF file form storage using AndroidPdfViewer library
- Opening a PDF file from the internet using AndroidPdfViewer library
So, for the first point, we will use WebViewActivity and for 2nd, 3rd, and 4th point, we will be using PdfViewActivtiy .
Create two activities named WebViewActivtiy and PdfViewActivity .(right-click on root directory > new > Activity> Empty Activity)
Now, for the above four actions, create four buttons and assign the task to open activity by those four buttons.
The code for the activity_main.xml file is:
In the MainActivity.kt file, we call the desired activity with the corresponding buttons:
We are done with the UI part. Let’s learn how to view PDF from WebView.
Opening a PDF file in Android using WebView
The very first and the easiest way of displaying the PDF file is to display it in the WebView. All you need to do is just put WebView in your layout and load the desired URL by using the webView.loadUrl() function.
So, add a WebView in the activity_web_view.xml file:
Now, open the FileUtils class of the utils package that we created at the starting of this blog and add a function named getPdfUrl that will return the URL of the PDF which we are going to view in the WebView. Here, I am using the syllabus of MindOrks Professional Course. Add below method in FileUtils class:
Now, all we need to do is open the above URL in the WebView by calling the webView.loadUrl method. Following is the code of WebViewActivity.kt file:
The last thing that you need to do is adding INTERNET permission to your application. So, add the below line in your AndroidManifest.xml file:
Now, run the application on your mobile phone and the PDF will be displayed on the screen.
Note: The opening of PDF in WebView depends on your internet speed, so wait for sometimes if your internet is slow.
Opening a PDF file in Android using AndroidPdfViewer library
There are various libraries that can be used to display PDF files in our application. In our tutorial, we will learn how to open a PDF file from Assets, Phone Storage, and from the Internet by using the AndroidPdfViewer library.
Also, we will be using PRDownloader library by MindOrks to download files from the Internet and open it by AndoridPdfViewer .
Adding dependencies and permissions
Open the app level build.gradle and add the below dependencies of AndroidPdfViewer and PRDownloader:
Since we will be reading pdf from INTERNET. Open the AndoidManifest.xml file and add the below:
The AndroiPdfViewer provides a PDFView to display PDF files in it. So, write the below code in actvity_pdf_view.xml :
We can use the AndroidPdfViewer to open the PDF from:
- Assets folder
- Phone storage
- Internet
So, we need to write the code to connect the button click of MainActivity with the above events. Create a function named checkPdfAction() and write the below code:
Call the above method from the onCreate() :
Let’s learn how to display PDF from assets, storage, and internet.
Assets Folder
Firstly, we will look upon how to view PDF, stored in the Assets Folder.
Creating an assets folder
Create an assets folder by right-clicking on main > New Folder > Assets Folder and paste the PDF document into it.
PDF file name: MindOrks_Android_Online_Professional_Course-Syllabus.pdf
Create getPdfNameFromAssets() method
Create a method named getPdfNameFromAssets in the FileUtils class. This method will return the name of the PDF file present in the assets folder:
Now, in the PdfViewActivity.kt file, create a method showPdfFromAssets which will take the file name in string format and will use the fromAssets() method of AndroidPdfViewer library to display PDF:
Call the above method from the checkPdfAction and pass the file name by calling the getPdfnameFromAssets method of FileUtils class:
Finally, run the application on your mobile device and see the output.
From Phone Storage
Now, we will look upon how to open PDF files from the Phone’s storage. So, we have to launch an intent to find the file having PDF format and the selected file will be displayed in the PDFView by calling the fromUri method.
Create a function selectPdfFromStorage() in the PdfViewActivity.kt file and add the below code:
Once, the user selects a PDF, the onActivityResult will be called:
Now, create a method named showPdfFromUri that will take a Uri and display the PDF:
Now, you can check the output by running your application on your mobile device and select the desired PDF.
PDF from Internet
Lastly, our aim is to view the PDF files from the Internet. We will first download the PDF by using the PRDownloader and then use this file to display the PDF on your PdfViewActiviy by using the same process as used for Assets and Storage but here you have to use fromFile() to add display the PDF.
So, we need to download the file first by using the PRDownloader library. Initialise it in the onCreate() method of PdfViewActivity :
Now, you need to download the file from the INTERNET by using the download() method of PRDownloader. So, create a function named downloadPdfFromInternet() in the PdfViewActivity. This function will take the URL, directory path, and file name of the file to be downloaded.
The onDownloadComplete method will be called when the file is downloaded. So, call the showPdfFromFile method and pass the downloaded file to the method:
Finally, call the downloadPdfFromInternet method from the checkPdfAction method but we need the URL, directory name and file name of the file to be downloaded. We can get the URL by calling the getPdfUrl method of FileUtils class. Now, make a function getRootDirPath method in the FileUtils class that will return the root directory:
Now, call the downloadPdfFromInternet method from checkPdfAction method of PdfViewActivity :
Finally, run the application and try to verify all the three options i.e. assets, storage and internet options to view PDF in Android Application. Try to replace the PDF link used in the above example with your own PDF URL.
There are many other methods present in the AndroidPdfViewer library. You can explore all the methods from here.
Project source code and What next?
Do share this tutorial with your fellow developers to spread the knowledge. You can read more blogs on Android on our blogging website .
Источник
20+ Best Apps To Open All Type Of File Formats In Android
These are the best apps to open different types of file formats!
Android is an ingenious device that provides lots of features to its users. That’s why it’s most widely used in the world. Many apps were being developed to run on this OS that makes it more user-friendly.
Most often or not, we deal with files on Android that require a dedicated app to open. For example, you would need a PDF viewer to open PDF files. Similarly, you would need a file compressor app to open RAR or ZIP files.
List of Best Apps To Open All type of Files
So, in this post, we will be discussing different types of apps to open different types of files on an Android. Let’s check out.
1. Open Video files
- MX Player
To open all types of video formats on your Android device, MX Player is the best choice. It is one of the best media players for Android. This app is famous worldwide and has very positive reviews, ratings, and download rates in Google Play Store. - VLC for Android
The VLC media player is a free and open-source cross-platform multimedia player that plays most multimedia files and discs, devices, and network streaming protocols. This is the port of the VLC media player to the Android platform. VLC for Android can play any video and audio files and network streams and DVD ISOs, like the desktop version of VLC. - AC3 Video Player
We love to watch movies on our Android smartphones. However, an AC3 format is not supported in MX Player. AC3 is the audio format that sometimes fails to load on our device. Therefore, the AC3 player lets you play AC3 and DTS audio formats easily and without any plugins. - Video Player for Android
Video Player for Android, the best video player for android 2019 in the market. Its intelligent detection adaptive algorithm makes it more convenient for you to enjoy smoother, better-quality videos. It supports all popular video formats, including AVI, 3GP, M4V, MOV, MP4, WMV, RMVB, MKV, TS, MPG, FLV, etc. - BSPlayer
BSPlayer Free is the top hardware accelerated video player for Android smartphones and tablet PCs. The app supports multi-core hardware decoding features, hardware-accelerated playback and also supports streaming from network devices. This is one of the best video players you can have on your Android smartphone.
2. Open All Type Of Compressed Files (Zip, RAR, etc.)
- Solid Explorer File Manager
You often deal with compressed files that are not directly accessible on the file manager as you require some third-party app to use them. So, Solid Explorer is a file manager app that can open compress files like ZIP, RAR, etc. - Andro Zip
AndroZip File Manager helps you copy, delete, move, unzip/unpack and compress encrypted ZIP files. Not only that, but you can even search and organize your files, music, pictures, and folders just like you would do on your PC. It’s optimized for both phones and tablets. - RAR
RAR can create RAR and ZIP and unpack RAR, ZIP, TAR, GZ, BZ2, XZ, 7z, ISO, ARJ archives. The list of functions includes repair command for damaged ZIP and RAR files, benchmark function compatible with RARLAB’s WinRAR benchmark, recovery record, usual and recovery volumes, encryption, solid archives, utilizing multiple CPU cores to compress data. - ZArchiever
ZArchiver – is a program for archive management. It has a simple and functional interface. This app lets you decompress zip, rar, rar5, gzip, archives, etc. You can even create and decompress password-protected archives using ZArchiever.
3. Open All Type Of Document Files (PDF, DOCX, TXT, and all others)
- WPS Office + PDF
If you deal with lots of Docs files on your mobile, WPS Office + PDF could be the best pick for you. It’s basically an office suite app for Android that can load all office files like Word, Excel, PowerPoint, PDF, and more. - Polaris Office + PDF Editor
Polaris Office + PDF editor is another best office suite application for Android that you can use today. It is a free application compatible with MS Word, Excel, PowerPoint, and Adobe PDF. You can even edit PDF files with this application. - OfficeSuite + PDF Editor
OfficeSuite lets you easily view, edit, and create Word, Excel, and PowerPoint documents and perform advanced PDF operations. Complete with our integrated file manager, this is the most feature-rich mobile office solution available on Android. - Docs To Go
Work from anywhere: View, edit, and create Microsoft Office files & view Adobe PDF files on your Android smartphone and tablet. Docs To Go contains best-in-class document viewing & editing at no charge. Apart from that, you also get options for connecting to multiple cloud storage accounts, desktop file sync, and opening password-protected files available via in-app purchase.
4. Open All Type Of Audio Format (3GP, MP3, MP4, M4A, AAC, etc.)
- MortPlayer Music
multiplayer is easy to use music player for all those who prefer folder structure over tags. Play all media types supported by core Android (MP3, Ogg Vorbis, M4A, 3GP, MIDI, Wave) and some device-specific (WMA, FLAC), but only on devices that support them. It’s one of the best free music player app for android. - N7 Music Player
If you are dealing with audio files every day, N7 Music Player would be a perfect choice. It supports all types of audio formats on your Android device. Another cool thing about this app is its fresh and user-friendly interface. - Pi Music Player
Pi Music Player is a stunning Music Player, beautifully crafted with Material Design in mind and packed with remarkable, powerful features. It supports almost every audio format. The latest version of the Pi Music player also has an equalizer that lets you adjust the music per your wish. - Poweramp Music Player
Poweramp Music Player is one of the best and powerful music player app you can have on your Android Smartphone. This app uses Direct Volume Control by default. If you use this app on Stock Rom, it produces a much better audio output. It’s one of the best music players for android with an equalizer.
5. Open Image Files Of Any Format( JPEG, PNG, JPG, BMP, GIF)
- Google Photos
If you are using an Android, the chances are that you already have this app installed on your phone. Google Photos is a photo management app from Google that lets you organize and easily share photos. You also get a photo backup option with Google Photos. All your images will be uploaded to Google Photos and counted towards the free 15GB provided with each Google Account. - Perfect Viewer
Well, Perfect Viewer is a multi-purpose app for Android. Guess what? With Perfect Viewer, you can easily view images, comics, and ebooks. If we talk about the photos, Perfect Viewer supports almost all popular image formats like JPEG, GIF, PNG, MBP, WebP, etc. It also supports archive file formats like CBZ/ZIP, RAR/CBR, etc. - Album
The Album application brings new intuitive ways to browse high-quality photos and videos at blazing speed. It also provides instant viewing and sharing of photos on online services. Album is also one of the lightweight image viewer apps that you can use on Android. - A+ Gallery Photos & Videos
A+ Gallery is the world’s best photo gallery app for your Android phone. It is also the fastest app for viewing HD photos, search photos, and managing albums. Your photos and videos are automatically organized by when and where you take them. - Gallery
If you are looking for an image app that can open almost any photo format, Gallery is the best option. This app automatically organizes your images and videos according to date, time, events, and locations. This also features some smart features like it automatically highlights the best photos, identifies similar shots, etc.
So above is all about the best apps to open all types of file format on Android. I hope this article helped you! Please share it with your friends also. If you want to suggest to us any other apps, let us know in the comments.
Источник