gpt4 book ai didi

java - Android:无法提供结果,无法找到具有权限的提供者的元数据

转载 作者:行者123 更新时间:2023-12-02 01:00:18 26 4
gpt4 key购买 nike

我正在尝试使用 uCrop 裁剪图像并上传到 firebase。我想允许选择使用相机或文件进行输入。

val pickIntent = Intent()
pickIntent.type = "image/*"
pickIntent.action = Intent.ACTION_GET_CONTENT
val takePhotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val pickTitle = "Select or take a new Picture"
val chooserIntent = Intent.createChooser(pickIntent, pickTitle)
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS, arrayOf(takePhotoIntent)
)
startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE)

我的onActivityResult函数如下所示:

override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
): Unit {
super.onActivityResult(requestCode, resultCode, data)

if (resultCode === Activity.RESULT_OK) {
if (requestCode === REQUEST_IMAGE_CAPTURE) {
var photoFile: File? = null
try {
photoFile = createImageFile()
if(photoFile != null) Log.v("AFTER CREATE IMAGE FILE", "photoFILE " + photoFile.toString())
if (photoFile != null) {
var photoUri: Uri =
FileProvider.getUriForFile(this, this.packageName, photoFile)
Log.v("AFTER FILE URI", "photoUri " + photoUri.toString())
startCrop(photoUri)
}

} catch (ex: IOException) {
}

}
}
}

上述代码中对 photoFile 的 null 检查失败,并且我没有收到该 if 语句的任何日志。但是,由于缺乏权限,我遇到了以下错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:25 flg=0x1 }} to activity {com.gigforce.app/com.gigforce.app.modules.photoCrop.ui.main.PhotoCrop}: java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.gigforce.app

但我的 list 中有以下内容:

        <provider
android:name="androidx.core.content.FileProvider"
android:authorities="{applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>

file_paths.xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="." />
</paths>

此外,由于某种原因,文件被保存在

/storage/emulated/0/Android/data/com.gigforce.app/files/Pictures/IMG_20200318_192722_2465645043825713296.jpg

而不是 file_paths.xml 中定义的 .

最佳答案

FileProvider.getUriForFile(this, this.packageName, photoFile)

您正在使用 this.packageName 作为您的权限字符串。

您的实际权限字符串是:

android:authorities="{applicationId}.provider"

这些不匹配。因此,请使用:

FileProvider.getUriForFile(this, "${this.packageName}.provider", photoFile)

或者:

FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", photoFile)

(后者应该快一点)

关于java - Android:无法提供结果,无法找到具有权限的提供者的元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60745090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com