【问题标题】:How to create intent to choose image from gallery in Android 11?如何在 Android 11 中创建从图库中选择图像的意图?
【发布时间】:2021-02-15 02:17:43
【问题描述】:

我可以像这样在 Kotlin 中创建从 Android 库中选择图像的意图:

val intentGallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
if (intentGallery.resolveActivity(activity.packageManager) != null) {
    // Launch the intent
}

我需要在 Android 11 (API 30) 上的“AndroidManifest”文件中添加什么“查询”块才能使此代码正常工作?

将此代码添加到“AndroidManifest”文件中的“查询”块中,将使其正常工作。

<package android:name="com.google.android.apps.photos" />

但我想添加一个涵盖所有图片库的代码,而不仅仅是 Google 的。

参考: https://developer.android.com/training/basics/intents/package-visibility

【问题讨论】:

  • 谢谢CommonsWare,但我不相信这会奏效。结果将始终为空,并且始终以 ActivityNotFoundException 结束。这是 Android 11 中的一个非常新的东西。请查看我帖子中的链接。
  • Intent.ACTION_PICK 与包的可见性无关。请说出您遇到的问题。
  • 这是不正确的。例如,如果我将此代码添加到“AndroidManifest”文件中的“查询”块中,它会正常工作,但没有它就不会。 但我想添加一个涵盖所有图片库的代码,而不仅仅是 Google 的。
  • CommonsWare,您的回复不正确。我想出了回应,Jahongir 也发布了。祝你推销你的坏书好运。

标签: android kotlin android-intent android-manifest


【解决方案1】:
<queries>
    <intent>
        <action android:name="android.intent.action.PICK" />
        <data android:mimeType="vnd.android.cursor.dir/image" />
    </intent>
</queries>

我希望这会有所帮助。我使用了这个查询参数,它工作正常

【讨论】:

  • 非常感谢您的回复! mime 类型的正确或更好的值是:android:mimeType="image/jpeg"
  • android:mimeType="image/*" 更好
猜你喜欢
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多