E / PdfViewerActivity:fetchFile:file:java.io.FileNotFoundException:文件不存在

时间:2016-10-13 04:33:10

标签: android android-intent

我试图通过意图创建pdf后打开它。该文件存在且可读,但保存在apps目录中。

目前,文件以下列方式保存

        OutputStream out;
    try {
        //TODO: expose through a content provider
        out = mContext.openFileOutput(outputFileName, Context.MODE_WORLD_READABLE);
        document.writeTo(out);
        out.close();

并使用以下代码将文件发送给intent

    final Intent viewIntent = new Intent();
    viewIntent.setAction(Intent.ACTION_VIEW);
    viewIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
    mActivity.startActivity(viewIntent);

尝试打开它的应用是google驱动器查看器,我在Android监视器中看到

  

E / PdfViewerActivity:fetchFile:file:java.io.FileNotFoundException:文件不存在

之前有效,我不认为我已经更改了与此代码相关的任何内容,但我已更新了我的工具。我已经尝试将build.gradle中的buildToolsVersion更改回之前的版本以及支持库&com; android.support:appcompat-v7:22。+'和' com.android.support:设计:22。+'而不是版本24.2.1

我已经尝试使用调试器将文件设置为在发送意图之前可读但不起作用。

由于

1 个答案:

答案 0 :(得分:2)

我从Android 7 Behavior Changes文档开始找到答案。设置MODE_WORLD_READABLE时,不再允许共享文件,即使文档没有提及任何

enter image description here

我按照sharing files上的文档来解决问题,但文档中的示例代码比我需要的更复杂。我仍然需要将文件提供程序添加到AndroidManifest.xml以及创建fileprovider.xml。

结果代码现在是

    final Intent viewIntent = new Intent();
    viewIntent.setAction(Intent.ACTION_VIEW);
    Uri fileUri = FileProvider.getUriForFile(mActivity, "my.authority.fileprovider", file);
    viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    viewIntent.setDataAndType(fileUri, "application/pdf");
相关问题