android - 文件提供程序异常:找不到配置的根目录,其中包含
问题描述
我正在尝试使用相机意图捕获图像并将其发送到服务器。
我遵循了官方文档https://developer.android.com/training/camera/photobasics
但是在使用 FileProvider 从文件路径中获取 Uri 时出现异常。
我遇到了一个例外
找不到包含 /storage/emulated/0/Android/data/com.example.app/files/Pictures/JPEG_20200310_160944_8900302509758571991.jpg 的已配置根目录
代码:
文件路径.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Android/data/com.example.app/files" />
</paths>
AndroidManifest.xml
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.app.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
相机活动.kt
private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
takePictureIntent.resolveActivity(packageManager)?.also {
val photoFile: File? = try {
viewModel.createFile()
} catch (ex: IOException) {
// Error occurred while creating the File
null
}
photoFile?.also {
photoURI= FileProvider.getUriForFile(
this,
"com.example.app.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
}
}
}
}
请帮我弄清楚我错在哪里。
尽管我已经检查了 stackoverflow 上的可能答案,但无法解决。
解决方案
代替:
path="Android/data/com.example.app/files"
和:
path="."
<external-files-path>
已经指向您在 中标识的位置path
。
推荐阅读
- sql-server - SQL Server:从一个表复制到另一个 accountID 匹配的表
- javascript - 在反应中使用 REST API 的正确方法?
- azerothcore - 在突袭中将光环应用于玩家的正确方法是什么?
- windows - 使用 Cygwin 和 Intel Parallel Studio 编译 IPOPT
- amazon-web-services - 动态创建新的子域并指向指向 s3 存储桶的云端分发
- excel - 如何为某个地图设置文件目的地?
- c++ - 将 std::variant 的包装器的 std::vector 传递给可变参数类,在不知道细节的情况下包装可变参数方法
- c - 为什么 getchar() 只在按下另一个键后返回 (char)13 ?(视窗)
- asp.net - 在 ASP.Net Core 中将多个数据表组合成单个结果
- sql-server - 在 SQL Server 的巨大堆表上创建主键的最佳方法