代码之家  ›  专栏  ›  技术社区  ›  mtmurdock

android:使用内置的pdf查看器从我的应用程序打开pdf

  •  28
  • mtmurdock  · 技术社区  · 16 年前

    这是我最初的问题:

    在我的应用程序中使用android的内置 pdf浏览器应用程序,但我不知道如何 打电话开始活动,我只是不知道 如何识别应用程序im打开和关闭 如何将文件传递给特定的 应用程序。

    有人有线索吗?

    9 回复  |  直到 16 年前
        1
  •  16
  •   CommonsWare    16 年前

    另外,Adobe还没有记录任何公开的 Intents 它希望开发者使用。

    你可以试一试 ACTION_VIEW Intent Uri 指向文件(在SD卡上或 MODE_WORLD_READABLE 在您的应用程序(本地文件存储)和MIME类型的 "application/pdf"

        2
  •  42
  •   Karakuri    14 年前

    您可以通过编程方式确定用户设备上是否存在合适的应用程序,而不会捕获异常。

    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
    }
    
        3
  •  5
  •   Flexo - Save the data dump sunny moon    14 年前
    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;
        }
    }
    
        4
  •  3
  •   Karan Mavadhiya    13 年前
                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);             
    
                }
    
        5
  •  2
  •   david.schreiber    12 年前

    虽然这是一个非常古老的主题,但这里有一个使用外部PDF阅读器应用程序打开资产/文件夹中的PDF的解决方案。它使用自定义内容提供程序: https://github.com/commonsguy/cwac-provider

        6
  •  2
  •   paxbat14    10 年前

    当我尝试在android设备上显示PDF时,也遇到了同样的问题,最终得到了解决方案(第三方PDF库集成)

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

    虽然我已经测试了下面列出的多个库,这些库也在工作,

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

    &带有ndk味道的mupdf( https://code.google.com/p/mupdf/downloads/detail?name=mupdf-1.2-source.zip&can=2&q= http://dixitpatel.com/integrating-pdf-in-android-application/

        7
  •  2
  •   JosephH    6 年前

    Android有一个来自android5.0/Lollipop的内置框架,叫做 PDFRenderer . 如果你可以假设你的用户有android5.0,那么它可能是最好的解决方案。

    谷歌开发者网站上有一个官方的例子:

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

    mupdf .

        8
  •  0
  •   Sundara Prabu    9 年前

    除了标记为answer的权限之外,您还需要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" />
    

    **

        9
  •  0
  •   Rezaul Karim    8 年前

    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);
    

    同时在res->处添加provider\u paths.xml;xml文件夹

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