【发布时间】:2017-12-21 18:58:01
【问题描述】:
我已更新我的项目以实施 Providers。整个实现似乎是正确的,但我在 FileProvider 类中收到了一个 StringIndexOutOfBoundsException 异常。
在我的例子中,创建了一个 Intent,用户将在其中选择从他的库中获取图像或拍摄新照片。
这就是我的一个 Intent 被创建的地方,之后,将其包含在一个列表中,以创建一个选择器。
File file = getTempFile(context);
if (file != null) {
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
takePhotoIntent.putExtra("return-data", true);
takePhotoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
FileProvider.getUriForFile(context, // line 43
BuildConfig.APPLICATION_ID.concat(".fileprovider"),
file));
intentList = addIntentsToList(context, intentList, takePhotoIntent);
}
getTempFile方法:
private static File getTempFile(Context context) {
File imageFile = new File(context.getExternalCacheDir(), TEMP_IMAGE_NAME);
if (imageFile.getParentFile().mkdirs()) {
return imageFile;
} else {
return null;
}
}
抛出的异常:
java.lang.StringIndexOutOfBoundsException: length=86; index=87
at java.lang.String.substring(String.java:1939)
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:728)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:404)
at com.project.helper.ImagePicker.getPickImageIntent(ImagePicker.java:43)
在这种情况下我该如何处理?也许这是这个版本的 Provider 中的一个错误?接下来,关于我的项目的更多信息:
清单文件:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.project.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
file_paths.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="images" path="Android/data/com.project/cache/tempImage" />
</paths>
buildToolsVersion '27.0.2'
【问题讨论】:
标签: android android-intent android-fileprovider