9/18/2023 0 Comments Intent android studioMain Interface of PDF Intent Options to open PDF PDF can be read using Intent method Wrap up Before that, keep the PDF in the specified location on your device. Once the application is ready, install it on your device and test the functionality. provider to declare the location you are going to access in that’s a reason for the following error: “android.os.FileUriExposedException: file:///storage/emulated/0/TRENDOCEANS.pdf exposed beyond app through Intent.getData()”īefore running the application, please verify the code of MainActivity. Inside openPDF(), you need to specify the location of the PDF file using the File class and save the value in a file variable, which you will use in uriPdfPath to declare the path after that, using Intent, you can call an external app to read the PDF.Įarlier, we used “Uri path = Uri.fromFile(file) ” to get the URI of a location, but it’s not working anymore, so you have to use “Uri uriPdfPath = FileProvider.getUriForFile()” and specify the. If it’s – 1, then it will prompt you to accept the permission.įor Android 12, devices need extra permission to read all files, and that’s why we will use Intent to ask permission to read all file access permissions. The first step will be used to check the current status of permission. This verifyStoragePermission method is used to request and verify read permission. Under the click listener, create a new method called openPDF (). After that, initialize the button to set a click listener. Inside the onCreate method, you need to call verifyStoragePermission (this). For that, you can simply copy the below code and paste it into your editor. ![]() ![]() Modify MainActivity to Open PDF File in External Applicationsįirst, you need to add a static constant value for permission check under the MainActivity class. If you are using Android 12, you will see two types of permission available compared to the previous Android version when you want to read files.Īnd copy the below content to to read the root directory of the external storage with the “external_file” name. Īnd the last is necessary for the latest Android version. To know more about this read package filtering. The first tag will add the android:name=”_ALL_PACKAGES” to list out the installed applications that can handle or read this pdf file. Now you will modify AndroidManifest.xml to add and tags nested with the tag. Create a Basic Layout in activity_main.xmlĪfter creating a new project, the first thing you need to do is modify activity_main.xml to add a button that will invoke the openPDF() method, which we are going to create in the MainActivity file.Īnd the rest of the options are just for visual appeal, which you can skip for now. Still, if you want, take a glimpse at how to create a new project in Android Studio, which is very similar to this project. I’m not going to show you how to create a new project in Android Studio because you know how to do it very well.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |