如何在Android 11上启动startActivityForResult? [英] How to startActivityForResult on Android 11?

查看:291
本文介绍了如何在Android 11上启动startActivityForResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 startActivityForResult()将图像加载到我的应用程序中,方法是这样:

I am loading images into my app using startActivityForResult() by doing something like this:

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,并且加载图像的活动未开始.

I have overridden onActivityResult() with my logic for loading the image into an ImageView. It works on all android versions (my app's minSdkVersion is 21) but it does absolutely nothing on Android 11. Because intentForLoadingImage.resolveActivity(packageManager) returns null and the activity for loading images doesn't start.

推荐答案

我了解了

Android 11更改了应用程序如何查询用户已在设备上安装的其他应用程序并与之交互的方式.使用该元素,应用程序可以定义他们可以访问的其他软件包的集合.通过告诉系统哪些其他程序包对您的应用程序可见,该元素有助于鼓励最小特权原则,并帮助Google Play等应用程序商店评估您的应用程序为用户提供的隐私和安全性.

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.

如果您的应用定位到Android 11或更高版本,则可能需要在应用的清单文件中添加元素.在该元素内,您可以通过名称,意图签名或提供者授权来指定软件包.

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.

因此,我在清单文件中添加了以下标记:

So, I added the following tags in my manifest file:

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

就是这样!

这篇关于如何在Android 11上启动startActivityForResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆