gpt4 book ai didi

android - 如何在 Android 11 上启动ActivityForResult?

转载 作者:行者123 更新时间:2023-12-04 16:25:52 25 4
gpt4 key购买 nike

我正在使用 startActivityForResult() 通过执行以下操作将图像加载到我的应用程序中:

val intentForLoadingImage = Intent(Intent.ACTION_GET_CONTENT)
intentForLoadingImage.type = "image/*"
if (intentForLoadingImage.resolveActivity(packageManager) != null) {
startActivityForResult(intentForLoadingImage, IMAGE_REQUEST_CODE)
}

我已经用我的逻辑覆盖了 onActivityResult() 以将图像加载到 ImageView 中。它适用于所有 android 版本(我的应用程序的 minSdkVersion 为 21),但它在 Android 11 上完全没有任何作用。因为 intentForLoadingImage.resolveActivity(packageManager) 返回 null 并且加载图像的 Activity 没有启动。

最佳答案

我了解到package visibility privacy updates on Android 11 .其中说,

Android 11 changes how apps can query and interact with other apps that the user has installed on a device. Using the element, apps can define the set of other packages that they can access. This element helps encourage the principle of least privilege by telling the system which other packages to make visible to your app, and it helps app stores like Google Play assess the privacy and security that your app provides for users.

If your app targets Android 11 or higher, you might need to add the element in your app's manifest file. Within the element, you can specify packages by name, by intent signature, or by provider authority.

所以,我在 list 文件中添加了以下标签:

<queries>
<intent>
<action android:name="android.intent.action.GET_CONTENT" />
<data android:mimeType="image/*"/>
</intent>
</queries>

就是这样!

关于android - 如何在 Android 11 上启动ActivityForResult?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64365455/

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