【问题标题】:Opening pdf file programmatically is going into menu page?以编程方式打开 pdf 文件将进入菜单页面?
【发布时间】:2011-11-22 03:18:38
【问题描述】:

为什么每次我尝试使用以下代码在我的 SDCARD 中打开一个 pdf 文件时,它实际上并没有打开 pdf 文件本身,而是进入了 adobe reader 的菜单?我的代码有什么问题吗?

Intent intent = new Intent();
    File pdfFile = new File("/sdcard/sample.pdf");
    Uri path = Uri.fromFile(pdfFile);
    intent.setAction(Intent.ACTION_VIEW); 
    intent.setData(path);
    intent.setType("application/pdf");
    intent.setPackage("com.adobe.reader");
    startActivity(intent);

【问题讨论】:

    标签: android pdf android-intent


    【解决方案1】:

    不,没有错。您已将类型设置为 pdf 并将包指定为 adobe.reader。这样做的目的是激发在 adobe reader 中启动 pdf 的意图。如果不使用库(或自己编写代码)来呈现 PDF 并显示它们,则无法直接在您的应用程序中显示 pdf。

    请注意,如果您只设置类型而不设置包,系统将查找可用于显示 PDF 的任何应用程序

    编辑

    你可以试试

        Intent intent = new Intent(Intent.ACTION_VIEW);
        File file = new File( filename  );
        intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
        startActivity(intent);
    

            Intent intent = new Intent();
            intent.setPackage("com.adobe.reader");
            intent.setDataAndType(Uri.fromFile(file), "application/pdf");
            startActivity(intent);
    

    【讨论】:

    • 那如何在没有指定包的情况下,从弹出的弹出窗口中选择一个选项时,它能够正确渲染?
    • 哦,我误解了这个问题。问题可能是您没有为 Adob​​e 提供足够具体的软件包。你只给它根包。我已经编辑了我的答案。
    • 所以如果我希望它使用上面的代码完全呈现,我将不得不使用 intent.setPackage("com.adobe.reader.????"?
    • 其实,我不确定。只需在我的回答中尝试我上面的一种可能性。
    • @LuxuryMode 谢谢...你节省了我的时间。
    【解决方案2】:

    我在这里给出我的 pdf 文件名。你给你的文件名。

        private static String FILE = Environment.getExternalStorageDirectory().getPath()+"/TestPDF.pdf";   
    
        File file = new File(FILE);
    
        if (file.exists()) {
            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
            try {
                startActivity(intent);
            } 
            catch (ActivityNotFoundException e) {
                Toast.makeText(this, 
                    "No Application Available to View PDF", 
                    Toast.LENGTH_SHORT).show();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 2020-05-05
      • 2023-04-05
      • 2023-03-19
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多