打开docx或pdf会导致Android设备文件读取错误

时间:2017-01-18 12:53:55

标签: android pdf docx

我创建了一个应用程序,其中我在Android设备中显示 pdf docx 文件,当点击按钮时我们在设备中下载我们正在显示它设备为何使用以下代码。

File pdfFile = new File(Environment.getExternalStorageDirectory() + "/FILE/a.docx");
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path,"application/msword");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(pdfIntent);

在Android 4.2中收到错误消息:

  

文件读取错误。文件类型不受支持或文件已损坏。

1 个答案:

答案 0 :(得分:0)

这不是DOCX文件的正确MIME类型。这是旧DOC格式的MIME类型。试试application/vnd.openxmlformats-officedocument.wordprocessingml.document

此外,不要使用连接来构建文件路径,因为您正在假设getExternalStorageDirectory()返回的值。使用:

File pdfFile = new File(Environment.getExternalStorageDirectory(), "FILE/a.docx");