Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 其他活动正在打开位于应用程序目录中的文档_Java_Android_Android Intent - Fatal编程技术网

Java 其他活动正在打开位于应用程序目录中的文档

Java 其他活动正在打开位于应用程序目录中的文档,java,android,android-intent,Java,Android,Android Intent,我有以下代码尝试使用默认读取器打开文件: Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.addFlag(Intent.FLAG_GRANT_READ_URI_PERMISSION); // type can be "application/msword" or "application/pdf" intent.setDataAndType(Uri.fromFil

我有以下代码尝试使用默认读取器打开文件:

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.addFlag(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// type can be "application/msword" or "application/pdf"
intent.setDataAndType(Uri.fromFile(file), type);
activity.startActivity(intent);
文件位于Android设备上的应用程序文件夹中:

/data/data/air.com.foo.foo/com.foo.foo/Local Store/folder1/file.ppt
问题是启动的活动无法找到文件。我在调用方代码中检查了文件是否存在,所以这不是问题所在

此外,如果我将文件从应用程序文件夹复制到应用程序共享的文件夹(SD卡上),则“活动”会找到该文件

我不明白为什么会失败,因为我添加了:

intent.addFlag(Intent.FLAG_GRANT_READ_URI_PERMISSION);

问题是,将文件复制到共享文件夹对我来说不是一个令人满意的解决方案,因为我希望它保留在我的初始应用程序文件夹中。

在有权访问该文件的活动中,您可能需要执行以下操作:

file.setReadable(true, false);
真是为了可读性,假是为了“仅限所有者”


由于您可以找到它,但无法阅读它,因此缺少此权限似乎是问题所在。

Intent.addFlag对您没有帮助。这是文件本身的权限问题。我们能看到启动的活动的代码吗?启动的活动不是我的,因为它可以是pdf阅读器、office阅读器……那么这是一个文件权限问题。确保其他应用程序可以读取该文件(请参阅下面的建议答案)。如果必须的话,请编写一个测试应用程序,尝试读取文件。你能看到问题出在哪里吗?谢谢,我尝试了这个,但没有成功。仅当文件复制到sddata文件夹时,此选项才有效