FileProvider.getUriForFile is causing StringIndexOutOfBoundsException

故事扮演 提交于 2019-12-13 18:24:40

问题


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

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.

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 method:

private static File getTempFile(Context context) {
    File imageFile = new File(context.getExternalCacheDir(), TEMP_IMAGE_NAME);
    if (imageFile.getParentFile().mkdirs()) {
        return imageFile;
    } else {
        return null;
    }
}

Exceptions that are thrown:

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)

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:

Manifest file:

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

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="images" path="Android/data/com.project/cache/tempImage" />
</paths>

buildToolsVersion '27.0.2'


回答1:


Replace:

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

with:

<external-cache-path name="images" 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.




回答2:


You have to change the xml in this way:

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


来源:https://stackoverflow.com/questions/47931146/fileprovider-geturiforfile-is-causing-stringindexoutofboundsexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!