【问题标题】:FileProvider.getUriForFile is causing StringIndexOutOfBoundsExceptionFileProvider.getUriForFile 导致 StringIndexOutOfBoundsException
【发布时间】: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


    【解决方案1】:

    你必须以这种方式更改xml:

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

    【讨论】:

      【解决方案2】:

      替换:

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

      与:

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

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

      【讨论】:

      • 在没有path参数的情况下更改为external-cache-path会导致path' attribute should be defined
      • @GuilhermeFGL:嗯...我认为这是可选的。试试path="."。或者,将您的图像放在getExternalCacheDir() 的某个子目录中,然后将该目录名称放在path 中。
      • &lt;external-cache-path name="images" path="." /&gt; 解决了我的问题。不过,不明白为什么会出现异常。
      • @GuilhermeFGL:那里的代码将文件的完全限定路径与应该是目录的路径进行比较。在你的情况下,你指向一个文件,这破坏了他们的算法。
      猜你喜欢
      • 2019-06-20
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多