Gui*_*FGL 5 android android-intent android-fileprovider
我已经更新了我的项目来实施Providers。整个实现看起来是正确的,但我StringIndexOutOfBoundsException在FileProvider课堂上收到异常。
在我的例子中,创建了一个意图,用户可以在其中选择从他的图库中获取图像还是拍摄新照片。
这就是创建我的意图之一的地方,然后将其包含在列表中以创建选择器。
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);
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
抛出的异常:
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)
Run Code Online (Sandbox Code Playgroud)
在这种情况下我该如何进行?也许这是这个版本的 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>
Run Code Online (Sandbox Code Playgroud)
file_paths.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="images" path="Android/data/com.project/cache/tempImage" />
</paths>
Run Code Online (Sandbox Code Playgroud)
buildToolsVersion '27.0.2'
代替:
<external-path name="images" path="Android/data/com.project/cache/tempImage" />
Run Code Online (Sandbox Code Playgroud)
和:
<external-cache-path name="images" path="." />
Run Code Online (Sandbox Code Playgroud)
首先,这将更可靠,因为您当前的实现对点进行了假设getExternalCacheDir()。其次,path需要指向一个目录,而不是一个文件,我的猜测是,这就是事情发生的地方。
| 归档时间: |
|
| 查看次数: |
2573 次 |
| 最近记录: |