问题描述
首先要提到的是,问题的答案此处没有帮助.
源代码如下所示:
Intent intent = new Intent("com.mycompany.action.PICK_FILE");String authority = "com.mycompany.fileprovider";文件文件 = Environment.getExternalStorageDirectory();intent.setData(FileProvider.getUriForFile(this, authority, file));
FileProvider.getUriForFile
抛出异常,这里是堆栈跟踪:
java.lang.StringIndexOutOfBoundsException: length=15;指数=16在 java.lang.String.indexAndLength(String.java:500)在 java.lang.String.substring(String.java:1313)在 android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:687)在 android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
在 Application
标签内的 AndroidManifest.xml
中,我添加了以下内容:
file_paths.xml
的内容如下:
<external-path name="input_files" path="."/><external-path name="output_files" path="MyAppName/"/></路径>
output_files
路径不在此上下文中使用.
我已经通过
FileProvider.SimplePathStrategy.getUriForFile()
运行调试器.相关代码片段如下(第 683-688 行):
final String rootPath = mostSpecific.getValue().getPath();如果(rootPath.endsWith(/")){path = path.substring(rootPath.length());} 别的 {path = path.substring(rootPath.length() + 1);}
rootPath
被评估为 /storage/sdcard
这也是 path
的确切值,所以抛出异常也就不足为奇了.
我做错了什么?
更新:
按照下面的建议,传递的
java.io.File
不应该是目录.文档含糊不清,从不明确说明.解决方法是手动"创建 uri.于是
intent.setData(FileProvider.getUriForFile(this, authority, file));
应替换为:
intent.setData(Uri.parse("content://" + file.getAbsolutePath()));
解决方案
getUriForFile 的第三个参数,应该是带有文件名的文件的路径.这里有一个文档,请阅读.>
First thing to mention, answers to the question here do not help.
The source code looks like the following:
Intent intent = new Intent("com.mycompany.action.PICK_FILE");
String authority = "com.mycompany.fileprovider";
File file = Environment.getExternalStorageDirectory();
intent.setData(FileProvider.getUriForFile(this, authority, file));
FileProvider.getUriForFile
is throwing the exception and here is the stack trace:
java.lang.StringIndexOutOfBoundsException: length=15; index=16
at java.lang.String.indexAndLength(String.java:500)
at java.lang.String.substring(String.java:1313)
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:687)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
In
AndroidManifest.xml
inside of application
tag I have added the following:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mycompany.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
/>
</provider>
Content of
file_paths.xml
is the following:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="input_files" path="." />
<external-path name="output_files" path="MyAppName/" />
</paths>
output_files
path is not used in this context.
I have run debugger through
FileProvider.SimplePathStrategy.getUriForFile()
. Relevant code snippet is the following (lines 683-688):
final String rootPath = mostSpecific.getValue().getPath();
if (rootPath.endsWith("/")) {
path = path.substring(rootPath.length());
} else {
path = path.substring(rootPath.length() + 1);
}
rootPath
is evaluated as /storage/sdcard
which is also the exact value of path
, so it's not wonder that the exception is thrown.
What am I doing wrong?
UPDATE:
As suggested bellow, passed
java.io.File
should not be a directory. Docs are ambiguous and never say that explicitly. The workaround would be to create uri 'manually'. Thus
intent.setData(FileProvider.getUriForFile(this, authority, file));
should be replaced with:
intent.setData(Uri.parse("content://" + file.getAbsolutePath()));
解决方案
Third params to getUriForFile, should be your path to your file with file name. Here is an docs, read it.
这篇关于FileProvider.getUriForFile 正在抛出 StringIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!