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

前端 未结 9 1729
温柔的废话
温柔的废话 2020-11-29 04:46

This was my original question:

I want to be able to open a pdf file in my app using the android\'s built in pdf viewer app, but i dont know how to

相关标签:
9条回答
  • 2020-11-29 04:59
    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 05:07

    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/

    0 讨论(0)
  • 2020-11-29 05:12

    Android has a built in framework from Android 5.0 / Lollipop, it's called PDFRenderer. If you can 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.)

    0 讨论(0)
  • 2020-11-29 05:17

    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
    }
    
    0 讨论(0)
  • 2020-11-29 05:18

    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.

    0 讨论(0)
  • 2020-11-29 05:19

    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" />
    

    **

    0 讨论(0)
提交回复
热议问题