【问题标题】:PDF viewed can't open file - but only on tablet查看的 PDF 无法打开文件 - 但只能在平板电脑上打开
【发布时间】:2016-06-23 07:26:15
【问题描述】:

我有一个应用程序,它会启动一个 pdf 阅读器以允许用户查看应用程序选择的文档。它适用于我的手机,但不适用于三星平板电脑。意图似乎工作正常,读者应用的选择出现,但选择读者时,很短的时间稍后会显示错误消息“无法打开文件”。 同一个应用程序还可以启动浏览器和文本应用程序以显示其他文件,这在平板电脑上运行良好。所以我的文件引用都可以。清单中 WRITE 的外部存储设置为 OK。 当我在平板电脑(不是通过我的应用程序)上选择文档时,它会打开。我选择了主应用程序的 pdf 部分并将其提取到一个简单的 pdf 只读应用程序中,但仍然是同样的问题。 它似乎是特定于平板电脑的 - 任何人都可以帮助我吗?

Intent i = new Intent(Intent.ACTION_VIEW);
File file1 = new File(Environment.getExternalStorageDirectory() + "/Documents/A.pdf");
Uri ur=Uri.fromFile(file1);
i.setType("application/pdf");
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i) ;

【问题讨论】:

    标签: android pdf


    【解决方案1】:

    代替这段代码

    Intent i = new Intent(Intent.ACTION_VIEW);
    File file1 = new File(Environment.getExternalStorageDirectory() + "/Documents/A.pdf");
    Uri ur=Uri.fromFile(file1);
    i.setType("application/pdf");
    i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(i) ;
    

    这样使用

    File pdfFile = new File(Environment.getExternalStorageDirectory(),"namePdfFile.pdf");//File path
                if (pdfFile.exists()) //Checking for the file is exist or not
                {
                    Uri path = Uri.fromFile(pdfFile);
                    Intent objIntent = new Intent(Intent.ACTION_VIEW);
                    objIntent.setDataAndType(path, "application/pdf");
                    objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(objIntent);//Staring the pdf viewer
                } else {
    
                    Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();
    
                }
    

    在浏览器中阅读 PDF 使用下面的代码:

    WebView webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true); 
    String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
    webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
    

    【讨论】:

    • 感谢您的回答。我只是按照你说的做了,还为文件存在和文件存在放了一个 Toast。当我选择查看器时,它仍然显示“无法打开文件”
    • @Kalininskaya 您想在浏览器或应用程序中阅读PDF
    • @Ironman - 我试图在浏览器中执行此操作,本质上是因为它对我来说应该更容易 - 在应用程序内部将涉及大量代码和对我的学习。我认为“浏览器”是指例如 Adob​​e Reader?
    • @Kalininskaya 我已将浏览器代码放在我的更新答案中。
    • @Ironman 对不起,我给出了错误的答案。我试图在 Adob​​e Reader 等应用程序中阅读 PDF,而不是在浏览器中。感谢您提供代码,但我希望在使用阅读器查找 PDF 文件时得到一些帮助
    【解决方案2】:

    感谢大家的帮助。实际上,我只是对哑剧类型感到困惑。阅读此内容,一切都很好。应用程序按照 Ironman 发布的路线完美运行

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多