Cannot open pdf document within android app - android

I was wondering can anyone help me with a problem that I'm having. I've looked at many posts of the same issue but I still cannot resolve this problem. I have created a PDF document. I have placed my created PDF file into my res/raw folder (I have also tried placing in assets folder) and I'm trying to open it within my app. When I click the button, it opens the dialog that I can select to complete the action using adobe reader, amazon kindle or polaris office. But when I go to try and open the pdf, I get an error saying "The document cannot be opened because it is not a valid PDF document".
The following is the code I am using to try and open the pdf, has anyone any ideas on why this will not work?
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.user_manual_v1_0_0;
File file = new File(fileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);

It will not work because few, if any, PDF viewers are set up to open PDFs from resources, and none can read directly from assets.
You will be better served copying the file to the filesystem. If you copy it to external storage, you can just open up a PDF viewer on that. If you copy it to internal storage -- by default, private to your app -- you can create a ContentProvider to serve the PDF to the viewer. This sample application demonstrates the latter approach.

Be sure to not forget the permissions for your app to reach external files. Place these two lines in your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Related

Open file(Video) from external storage in local html (WebView) from assets folder? (Android)

I'm using web project with VR functionality implemented via A-frame and three.js to display VR/360 content with some additional functionality, the whole web project is located in assets folder, and successfully displays 360 images/videos from project directory (HTML is "source src="./default.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"').
Target is to replace video source with URI from device external memory (downloads directory for example).
I've tried to use URI's in following formats :
Android standard :
file:///storage/emulated/0/Download/Trafalgar_Square_360.mp4
URI formats found on StackOverflow :
file:///storage/extSdCard/Download/Trafalgar_Square_360.mp4
file:///sdcard/downloads/Trafalgar_Square_360.mp4
file:///localhostpage/Download/Trafalgar_Square_360.mp4
P.S. replacing vid URI to other from assets folder (example "./other_vid.mp4") works fine.
Hope you guys can give me some ideas on this topic, or at least explain if it's impossible.
add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> to your Manifest file.

How to open a PDF file onclick of a button in Android?

I need to save PDF file when my app installs and then open it when a user clicks on a button. I found many solutions but was not able to integrate them into one.
Try this code, display pdf file from a file location (works from any file location, not SDcard specific)
How to open a PDF via Intent from SD card
I was doing this in work today and it is not that easy.
1) saving the file is not difficult any normal file code will work for a pdf file.
2) showing the pdf you have two options you can display a pdf in a webview if the device has internet access by displaying a Webview and loading google docs with you pdf file at the end of the url
3) or you can create an intent and see if the device has an application that will display the file, you will need to use a FileProvider to get it to run but there is a good post
https://developer.android.com/reference/android/support/v4/content/FileProvider.html

Open pdf in external app

I have and android app. It has a pdf in assets folder. I written a code to view this pdf in external app(like adobe pdf viewer). It is working fine but problem is that there is Share option in Adobe pdf viewer. I dont want that user can distribute this pdf.
My problem is that there is an important pdf book and I want user can read only but can not share or redistribute it. Please give an idea. I will change my code.
I am using following code:
Uri path = Uri.fromFile(new File(str,
"lemmelibroandroid.pdf"));
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);
I dont think it is possible to limit the capabilities of the external application used to open the pdf
Have a look at
http://asmncl.blogspot.in/2012/06/android-open-pdf-file-in-webview.html to see how to open the pdf in a WebView
It is not possible,so the best option here will be to show your content in your own app.
I am sure that you will find a proper way to display it some how in activity.
Maybe you should change the format of PDF to HTML and it will be easier to show.

Can't open a downloaded PDF in Adobe or Viewer via Intent

I'm trying to open a .pdf via a new Intent. The intent fires and the pdf app selection fires. When I select either app. The app displays 'unable to open document' I am passing data/data/com.android.providers.downloads/cache/document.pdf as the Uri which I am suspecting is the problem. There is no sd card. The pdf will open fine via the dowloads folder. Any help is appreciated.
I am passing data/data/com.android.providers.downloads/cache/document.pdf as the Uri
That file is not readable by third party applications.
The pdf will open fine via the dowloads folder.
Your main choices are to store the PDF on external storage (such as "the dowloads folder"), or to create a ContentProvider that can serve the file out of internal storage. This sample project demonstrates such a ContentProvider.

Display PDF in webview

I want to show a pdf in my application. My webservice gives me the pdf and I want to download it to sd card and then I want show that pdf in my app through webview.
I was also struggling with Android PDF showing problem for some time. I did the same approach as you mentioned with downloading the PDF to the SD card, but I did not manage to open it up otherwise than using a preinstalled Android App which could do the PDF opening (e.g. Adobe Reader or similar). I think that it's not possible to view the PDF in a WebView widget. The reason is simply that the included webview does not support plugins like adobe reader (see: http://osdir.com/ml/Android-Developers/2010-09/msg03331.html)
You can easily open up the downloaded PDF using an Intent call:
File file = new File("/sdcard/filename.pdf");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Due to the fact that this technique opens up the PDF viewer directly after my application makes this call, the back button takes the user directly back to the app, so it feels like the pdf viewing is part of my app.
Hope this helps, best regards
As already answered here, the WebView can't render PDFs. You can do what it is said in the accepted answer in this question and try to open the PDF with the default PDF viewer in the device. If the device has no PDF viewer installed, a message will be displayed to the user.

Categories

Resources