【发布时间】: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