android: open a pdf from my app using the built in pdf viewer

梦想的初衷 提交于 2019-11-27 04:03:22

AFAIK, Adobe has not documented any public Intents it wants developers to use.

You can try an ACTION_VIEW Intent with a Uri pointing to the file (either on the SD card or MODE_WORLD_READABLE in your app-local file store) and a MIME type of "application/pdf".

You can programmatically determine whether a suitable application exists on the user's device, without catching exceptions.

Intent intent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("path-to-document"));
intent.setType("application/pdf");
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
    startActivity(intent);
} else {
    // Do something else here. Maybe pop up a Dialog or Toast
}
SALMAN
private void loadDocInReader(String doc)
     throws ActivityNotFoundException, Exception {

    try {
                Intent intent = new Intent();

                intent.setPackage("com.adobe.reader");
                intent.setDataAndType(Uri.parse(doc), "application/pdf");

                startActivity(intent);

    } catch (ActivityNotFoundException activityNotFoundException) {
                activityNotFoundException.printStackTrace();

                throw activityNotFoundException;
    } catch (Exception otherException) {
                otherException.printStackTrace();

                throw otherException;
    }
}
            FileFinalpath = SdCardpath + "/" + Filepath + Filename;
            File file = new File(FileFinalpath);
            if (file.exists()) {
                Uri filepath = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(filepath, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    startActivity(intent);
                } catch (Exception e) {
                    alert.showAlertDialog(PDF_Activity.this, "File Not Started...","File Not Started From SdCard ", false);             
                    Log.e("error", "" + e);
                }

            } else {
                alert.showAlertDialog(PDF_Activity.this, "File Not Found...","File Not Found From SdCard ", false);             

            }

Although this is a pretty old topic, here is a solution for opening a PDF that is in the asset/ folder with an external PDF reader app. It uses a custom content provider: https://github.com/commonsguy/cwac-provider

Using this you can define any file to be provided from the assets/ or res/raw/ folder.

Try it! Best and easiest solution I found so far.

I was also faced same issue when was trying to display PDF on android device and finally end up with the solution (3rd party PDF library integration)

https://github.com/JoanZapata/android-pdfview

while I have tested multiple libraries for this listed below which are also working,

https://github.com/jblough/Android-Pdf-Viewer-Library

& mupdf which comes with the ndk flavour (https://code.google.com/p/mupdf/downloads/detail?name=mupdf-1.2-source.zip&can=2&q=) and need to extract with NDK and then use it in application as a jar or java etc. nice article to explain the use of this library @ http://dixitpatel.com/integrating-pdf-in-android-application/

Android has a built in framework from Android 5.0 / Lollipop, it's called PDFRenderer. If can you make the assumption that your users have Android 5.0, it's probably the best solution.

There's an official example on Google's developer site:

http://developer.android.com/samples/PdfRendererBasic/index.html

It doesn't support annotation or other more advanced features; for those your really back to either using an Intent to open a full app, or embedding an SDK like mupdf.

(Disclaimer: I very occasionally do work on mupdf.)

Add FLAG_GRANT_READ_URI_PERMISSION

Intent intent = new Intent(Intent.ACTION_VIEW)
Uri outputFileUri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file); 
intent.setDataAndType(outputFileUri, "application/pdf"); 
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
Intent in = Intent.createChooser(intent, "Open File");
startActivity(in);

also add provider_paths.xml at res -> xml folder and need to add below code at manifests

<application>
   <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="android:authorities">
            <meta-data
                tools:replace="android:resource"
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
 </application>

In addition to the ones marked as answer you would need these permissions in the manifest.xml

**

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

**

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!