How to open a pdf stored either in res/raw or assets folder?
I’m going to show a pdf in my application, and the pdf has to be bundled with the application.
What is a good way to do this?
I have read that it might be possible to do this by adding the pdf file to a res/raw folder and read it from there, but i get project errors when i put the pdf file there.
So i tried to put the pdf file in the asset folder of the project, and it gave no errors.
This is how i’ve tried to show the pdf:
Any ideas or suggestions?
Thanks in advance
6 Answers 6
You cannot open the pdf file directly from the assets folder.You first have to write the file to sd card from assets folder and then read it from sd card.The code is as follows:-
Thats all..Enjoy!! and please dont forget to give +1.Thanks
You would be able to show it from raw/ or assets/ if your application actually implemented a PDF reader. Since you want it to be displayed in a separate application (such as Adobe Reader), I suggest doing the following:
- Store the PDF file in the assets/ directory.
- When the user wants to view it, copy it somewhere public. Look into openFileOutput or getExternalFilesDir .
- Launch the Intent just like you are doing now, except use getAbsolutePath() on the newly created file for the intent’s data.
Be aware that a user might not have a PDF reading application. In this case, it is useful to catch the ActivityNotFoundException and show an appropriate message.
Источник
how to open pdf file from assets folder
Trying to open pdf file from asset folder on clicking the button
by passing the string value of button
2 Answers 2
You can do this in Four steps ☺
Step 1 : Create assets folder in your project and Place the PDF in it
:: For example : assets/MyPdf.pdf
Step 2 : Place the following code in your class [onCreate] :
Step 3 : Place the following code in your Layout :
Step 4 : Permission :
Android Q update
This is an older question, but with Android Q there are some changes because of the new file access permission/system. Now it’s not possible anymore to just store the PDF file in a public folder. I solved this problem by creating a copy of the PDF file in the cache folder in data/data of my app. Whit this approach the permission WRITE_EXTERNAL_STORAGE is no longer required.
Open the PDF file:
Provider configuration inside provider_paths.xml for accessing the file outside of your own app. This allows access to all files in the cache folder:
Add the file provider configuration in your AndroidManifest.xml
This could be enhanced by copying the files only once and checking if the file already exists and replacing it. Since opening PDFs is not a big part of my app I just keep it in the cache folder and override it every time the user opens the PDF.
Источник
Read a pdf file from assets folder
I am trying to read a pdf file from assets folder which is present in my application folder . Everything is working perfectly when i click on my DOCS button a pop up comes to let me choose an application for opening the pdf i.e «abc.pdf» but after selecting an option i get an error message «The file path is not valid». I think their is some problem with the path which i have specified in the code. please help
11 Answers 11
Make sure to include
You can do it like this (tested and working on API 27)
Step 1
Add following dependency in your app gradle:
Step 2
Add following XML code:
Step 3
Add following code in your java file:
These changes will load a PDF file on creation of your activity.
Though this has been answered, I wanted to share my solution as I think it is a bit easier to include.
And here is the OpenLocalPDF class:
add this permission in Manifest.xml
like say sunsil, but in the case for external directory.
change parts of code like these:
the before example is for Pdfs, in case of to example .txt
I had success, using the answer from cYrixmorten, with the OpenLocalPDF class.
But, Environment.DIRECTORY_DOCUMENTS is not supported for Android 18. To support Android 18 — 28, I had to make the following changes.
In «CopyFileAsyncTask», change the fileRoot declaration:
In «onPostExecute», change this:
Finally, setup your manifest to support the FileProvider as described in this post.
No answer worked for me out-of-the-box when attempting to open a file via an intent (in my case PDF) from the assets folder. So here is my solution combining 2 brilliant answers I’m sure it will help someone. Must be for api >= 24.
Once you have followed this link and added the your own GenericFileProvider and declared it in your manifest within the Application tag and created the provider_paths.xml simply use the below class and call it like:
This is a way to open multiple PDF files from assets using a zip file.
Step1: Copy a zip file( pdf.zip ) into assets folder that contains the list of pdfs
Step2: Write PDF’s into external cache inside the app in a Utils class
Step3: Call the above function in Activities onCreate
Step4: Create a common function in Utils class to open the PDF
Step5: Call the openPDF function on click on the view in Activity
This works for me.
Step 1: In MainActivity.java
Step 2: Create provider_paths.xml inside res->xml->provider_paths.xml . See here
Step 3: In AndroidManifest.xml
If you want to open a .pdf file that is stored locally in the assets folder without using an Intent to launch an external app, I suggest using the Android class PdfRenderer. Documentation found here.
This is a good example that worked for me.
However, this example wouldn’t run when I downloaded it. I had to change it a bit to use the copyReadAssets() function as mentioned in the other answers here, then to reference the file (after it has been copied) I use:
I also ended up modifying onAttach() because it was using the deprecated form of onAttach() and closeRenderer() because it was throwing errors for closing objects that were null.
Источник
Opening pdf from assets
I’m currently trying to use dynamic delivery to download and install some pdf files and open them with barteksc’s pdf viewer ( com.github.barteksc:android-pdf-viewer:2.8.2 ).
When I had the pdf files inside my apk everything worked fine but now that I download them and install them via dynamic delivery I get this error when I try to open them
E/PDFView: load pdf error java.io.FileNotFoundException: Example.pdf at android.content.res.AssetManager.nativeOpenAsset(Native Method) at android.content.res.AssetManager.open(AssetManager.java:744) at android.content.res.AssetManager.open(AssetManager.java:721) at com.github.barteksc.pdfviewer.util.FileUtils.fileFromAsset(FileUtils.java:37) at com.github.barteksc.pdfviewer.source.AssetSource.createDocument(AssetSource.java:39) at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53) at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764)
This returns «Installed»
I am installing the module from a different Class if that makes a difference, I saw something about updating the context in Google docs but I could not figure it out.
Источник
Android — open pdf in external application
i’ve a pdf file in my app assets directory that i want open using an external app, so wrote my content provider and i’m tryng to make it work but nothing.
here is the code:
and here how i call the opening of the file:
i’ve added the following line in the android manifest, inside the tag:
this is the logcat output:
don’t understand where the problem is, seem that the external application can’t get the file.
thanks for any help!
3 Answers 3
My implementation is below. Note that if your filename is MyPdf.pdf, the file should be assets/public_pdfs/MyPdf.pdf.mp3. The path public_pdfs is to only export the pdfs you really want to export, and the .mp3 extension is to prevent compression.
AndroidManifest.xml
Opening a PDF
PdfContentProvider.java
I am new to android development and spent the whole day looking for the reason that my locally stored PDF could not be opened by external apps. I am glad having found this thread.
In the meantime Marco got it working and describes it here. Beware: Italian language- Google translation service might help 😉
Additional notes to his howto for beginners:
The line Marco mentions to be inserted in the Manifest.xml
should be inserted within the tag.
And if you plan to have custom filenames you should exchange
PDF files have to be put into already existing folder assets in your project (especially not in any newly added folder res/assets or sth.)! =)
Источник