【问题标题】:ActivityResultContract opens gallery twiceActivityResultContract 两次打开画廊
【发布时间】:2021-09-16 08:28:06
【问题描述】:

按照 Google https://developer.android.com/training/basics/intents/result 的建议,我目前正在使用 ActivityResultContract(由于弃用)在 Android 应用 (java) 上切换 ActivityResults。 Example code

当我实现相同的示例代码从图库中获取图像时,如上面的链接所示,activityResult 不仅会打开默认图像选择器,还会要求我选择一个应用程序来选择图像: App opening image chooser and asking to choose an app

有没有办法避免要求应用选择图像?

【问题讨论】:

  • 分享您的代码,这样我们也许可以提供帮助!
  • 图片是除了指向developer.android之外的另外两个链接,activityResultContract的调用与示例代码中的相同。
  • 欢迎@navi95!为了让社区更好地帮助您,请遵循stackoverflow.com/help/how-to-ask 指南。

标签: android android-studio android-activity


【解决方案1】:

您可以改用 ActivityResultLauncher。它将打开系统图库以从中选择图像。

只需按照以下步骤操作:-

  1. 首先像这样创建 ActivityResultLauncher:

    ActivityResultLauncher<String> mGetContent = 
             registerForActivityResult(new GetContent(), new ActivityResultCallback<Uri>() {
     @Override
     public void onActivityResult(Uri uri) {
       // Handle the returned Uri
     }
    });
    
  2. 然后,在按钮单击或您需要时启动系统图像选择器。

    mGetContent.launch("image/*");
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多