FileProvider.getUriForFile导致StringIndexOutOfBoundsException [英] FileProvider.getUriForFile is causing StringIndexOutOfBoundsException

查看:721
本文介绍了FileProvider.getUriForFile导致StringIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经更新了我的项目,以实现 Providers .整个实现似乎是正确的,但是我在FileProvider类内收到了StringIndexOutOfBoundsException异常.

I have updated my project to implement Providers. The whole implementations seem right, but I am receiving a StringIndexOutOfBoundsException exception inside FileProvider class.

在我的案例中,创建了一个Intent,在该Intent中,用户将在从图库中获取图像或拍摄新照片之间进行选择.

In my case, an Intent is created, in which, the user will choose between get an image from his library or take a new photo.

在这里创建我的一个Intent,然后将其包含在列表中以创建选择器.

That is where one of my Intents is created, to after, include in a list, to create a chooser.

    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中的错误?接下来,有关我的项目的更多信息:

How do I proceed in this situation? Maybe it is a bug in this version of Provider? To follow, some more information about my project:

清单文件:

    <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文件:

file_paths.xml file:

<?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需要指向目录,而不是文件,我的猜测是这是横向的.

First, that will be more reliable, as your current implementation makes assumptions about where getExternalCacheDir() points. Second, path needs to point to a directory, not a file, and my guess is that this is where things are going sideways.

这篇关于FileProvider.getUriForFile导致StringIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆