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

标签 android android-intent startactivityforresult loadimage

我正在使用 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/

相关文章:

android - 在 Kotlin 中创建微调器时,如何修复指定为非空的参数在 fragment 中的旋转屏幕上为空?

java - 安卓 : Using Broadcasting Receiver

Android ACTION_DATE_CHANGED 广播

java - 如何在Intent中发送模型的对象

android - 无法在 Unity Activity 中返回结果

Android insertWithOnConflict 返回错误值

android - 无法生成签名的 apk? [发生重复平台类错误]

c# - 将文本设置为剪贴板 Xamarin

android - ActivityResultLauncher 传递自定义请求代码

android - 如何使用 startActivityForResult 管理 Activity