- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经更新了我的项目以实现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/
我使用 Intent 发送文件的代码不适用于所有文件源,我还找不到解决方案: 我的应用程序已注册用于打开文件,因此当我在 ES 文件资源管理器中选择一个文件时, Intent 如下所示: intent
我正在制作一个应用程序,我需要拍照,然后将一些数据保存到数据库中,但是当我尝试从照片中获取 URI 时出现此错误: java.lang.IllegalArgumentException: Failed
FileProvider.getUriForFile 返回一个空对象引用,我不知道为什么。这以前有效。以下错误是我得到的。我还在错误下方添加了我的代码。 Process: com.example.jo
所以我正在开发一个应用程序,我会在其中拍照并尝试将其保存到应用程序的内部存储空间中。我在使用文件提供程序时遇到问题。我已经看过许多关于堆栈溢出的问题,但如果可能的话,我想得到更详细的解释。 我也按照谷
首先要提的是,问题的答案here不帮忙。 源代码如下所示: Intent intent = new Intent("com.mycompany.action.PICK_FILE"); Strin
在我的应用中,尝试使用相机应用捕捉图像。我正在更新我的应用程序以在指定用于保存图像的文件路径时使用 FileProvider.getUriForFile,因为 Uri.fromFile 已被弃用。 使
你能帮我调试我的应用程序吗,它构建时没有错误,但是一旦我单击“更改个人资料图片”加载相机(就像 Instagram 上用于更改个人资料图片的按钮),我的应用程序就会停止。 我将不胜感激任何帮助,谢谢
我已经更新了我的项目以实现Providers。整个实现似乎是正确的,但我在 FileProvider 类中收到了一个 StringIndexOutOfBoundsException 异常。 在我的例子
我正在尝试编写我的第一个 Android 应用程序,它将涉及拍照和处理照片。 我在网上看了几个教程后整理了一些代码,但是每当单击按钮时我都会收到以下 NullPointerException: 10-
我按照 android developer reference on FileProviders 上的示例代码进行操作但它不会工作。 我在 Manifest 的文件提供程序定义中设置了路径 files
在我的应用中使用 FileProvider.getUriForFile 时出现仅在华为设备上发生的异常: Exception: java.lang.IllegalArgumentException:
长话短说 我想通过 FileProvider 获取(读取/生成)文件 Uri - 路径如下 - 但不知道如何: val file = File("/external/file/9359") // ho
我正在尝试实现一个 FileProvider 以允许我与电子邮件和其他应用程序共享本地私有(private)图像。我一直按照 Android 开发人员指南的说明进行操作,并深入了解必须使用“getUr
目前,当文件位于外部 SD 上时,FileProvider getUriForFile 方法会生成 IllegalArgumentException 当文件在设备内存中(在/storage/emula
我在 Kotlin 中遇到了奇怪的行为 创建像这样的自定义文件提供程序时 import android.support.v4.content.FileProvider class MyFileProv
在我的 Andoroid 应用程序中,我想让用户使用用户喜欢的任何发送方法(例如电子邮件、Skype、Viber、蓝牙等)发送我的应用程序生成的文件我正在使用 Intent.ACTION_SEND 如
我正在尝试实现一个相机 Activity ,用户可以在其中拍照并将其保存到手机上。 错误: java.lang.IllegalArgumentException: Failed to find con
下面的代码用于尝试文件提供程序,为 Android Nougat 做准备: list 文件:
我一直在尝试关注 Android tutorial关于共享文件。我这样设置 FileProvider: 在主 list xml 上: res/xml/fi
我有来自 this 的代码统一回答代码以安装 apk,但它在 Android 7.0 中不起作用,因为不再支持 Uri.fromfile 并且现在应该使用 FileProvider.getUriFor
我是一名优秀的程序员,十分优秀!