【问题标题】:Using an Intent to open a local PDF file使用 Intent 打开本地 PDF 文件
【发布时间】:2015-10-18 21:24:41
【问题描述】:

我在互联网上搜索了一段时间,但没有找到解决方案!

在我的项目中,我将一个 PDF 文件放在了可绘制文件夹中(老实说,我不知道该放在哪里)。该 PDF 是一个菜单,向用户显示他可以在该餐厅找到的所有食物。 有一个按钮可以让用户打开该 PDF 文件。通过单击它,我会收到一条错误消息。它或多或少说应用程序无法归档我的文件:“Impossibile to open menuristorante.pdf”。

我创建了一个方法来打开该文件,这是我编写的代码:

public void openMenuRistorante(View view)
{
File pdfFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/menuristorante.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try{
    startActivity(pdfIntent);
}catch(ActivityNotFoundException e){
    Toast.makeText(this, "Nessuna Applicazione per leggere i pdf", Toast.LENGTH_SHORT).show();
}
}

可能我将文件放在错误的目录中是错误的。但是我应该把它放在哪里?请记住,我的 PDF 文件必须已经在应用程序中,因此当用户安装此应用程序时,它必须在手机中。

谢谢

【问题讨论】:

  • 将pdf文件放入assets或raw文件夹,然后设置assets文件夹的路径。 pdf 将打开,没有任何错误。
  • 将文件放入assets文件夹。在运行时将文件从资产复制到外部存储器。然后使用外部存储器中文件的完整路径。
  • The pdf will be opened without any error。我很确定它不会。

标签: java android eclipse pdf android-intent


【解决方案1】:

将 PDF 文件放入 assets 文件夹并尝试使用以下代码:

Uri file= Uri.parse("file:///android_asset/mypdf.pdf");
  String mimeType =  MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(file.toString()));

try {
     Intent i;
     i = new Intent(Intent.ACTION_VIEW);
     i.setDataAndType(file,mimeType);
     startActivity(i);

} catch (ActivityNotFoundException e) {
                    Toast.makeText(this, 
                        "No Application Available to view this file type", 
                        Toast.LENGTH_SHORT).show();
                } 

【讨论】:

  • 无事可做...我尝试了您提供给我的所有代码....但我收到了相同的错误消息。我在 APP -> SRC -> MAIN -> ASSETS 中创建了“assets”文件夹,在这个文件夹中我放了 2 个文件:“menu.pdf”和“prova.doc”,使用你在 Uri 文件之前给我的代码=Uri.parse( ................... ) bla bla bla 我试图打开 DOC 文件,但现在收到此消息:%s 未找到。所以它似乎甚至没有看到我的文件!
  • GreenApps,它实际上不起作用......你能帮帮我吗?我真的不知道该怎么办
  • 我已经告诉过你该怎么做。所以请继续。有什么问题?
  • APP -> SRC -> MAIN -> ASSETS 大写?
【解决方案2】:

我在我的应用程序中做类似的事情,我的文件虽然在我手机的下载文件夹中的一个名为“文档”的子文件夹中,我想你也可以这样做,这比保留它更好可绘制的。这是我使用的代码:

try {
            File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/Documents/" + fileName);
            Intent intent = new Intent("com.adobe.reader");
            intent.setType("application/pdf");
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/pdf");
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(Documents.this, "Erreur d'ouverture du fichier", Toast.LENGTH_LONG).show();
        }

【讨论】:

  • getExternalStoragePublicDirectory 已被弃用。
猜你喜欢
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多