gpt4 book ai didi

android - FileProvider.getUriForFile 导致 StringIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-29 16:46:21 25 4
gpt4 key购买 nike

我已经更新了我的项目以实现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 的一个错误?接下来是关于我的项目的更多信息:

list 文件:

    <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'

最佳答案

替换:

<external-path name="images" path="Android/data/com.project/cache/tempImage" />

与:

<external-cache-path name="images" path="." />

首先,这会更可靠,因为您当前的实现假设了 getExternalCacheDir() 指向的位置。其次,path 需要指向一个目录,而不是一个文件,我猜这就是事情发展的方向。

关于android - FileProvider.getUriForFile 导致 StringIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47931146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com