【问题标题】:Android intent to open gallery doesn't work on KitKat (4.4)Android 打开图库的意图在 KitKat (4.4) 上不起作用
【发布时间】:2014-02-04 18:00:51
【问题描述】:

我正在开发一个应用程序,该应用程序需要允许用户选择个人资料图片,并且我想为他们提供一个简单的选项来拍照或从图库中选择现有的照片。

我的搜索将我带到了this Stackoverflow discussion,因此我出于我的目的修改了其中一个答案中的代码。这是我所拥有的:

public static Uri openImageIntent(Activity context) {
    Uri outputFileUri = null;
    File cache_dir = context.getExternalFilesDir("photos");

    cache_dir.mkdirs();

    File image_file = null;
    try {
        image_file = File.createTempFile("profile", ".jpg", cache_dir);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (image_file != null) {
        File sdImageMainDirectory = new File(cache_dir.getAbsolutePath(), image_file.getName());
        outputFileUri = Uri.fromFile(sdImageMainDirectory);

        // Camera.
        List<Intent> cameraIntents = new ArrayList<Intent>();
        Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);

        for(ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            cameraIntents.add(intent);
        }

        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

        // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));

        context.startActivityForResult(chooserIntent, Constants.RequestCodes.CHOOSE_PICTURE);
    }

    return outputFileUri;
 }

如您所见,它通过组合 ACTION_IMAGE_CAPTURE 和 ACTION_GET_CONTENT 意图创建了一个选择器,后者被过滤为“image/*”。这里的目标是打开一个自定义选择,让用户可以选择相机应用程序来拍摄新照片,或者选择图库应用程序(或照片,或文件浏览器)来选择现有图像。我还必须在 onActivityResult() 中做一些工作才能正确接收图片并根据选择使用它,但我认为这与这里无关。

在 4.3 及更低版本上,这很好用。它会打开一个看起来像这样的选择器:

然而,在 KitKat 上,选择器看起来像这样:

如您所见,它似乎忽略了“image/*”过滤器,只是给了我一些通用的“文档”应用程序来打开文件。

显然 KitKat 发生了一些变化,但我不知道是什么,也找不到其他遇到此问题的人。

有什么想法吗?

【问题讨论】:

  • 这很奇怪。 Gallery 仍然显示它在运行 Android 4.4.2 的 Nexus 4 上支持 ACTION_GET_CONTENTimage/*。如果您暂时注释掉您的 EXTRA_INITIAL_INTENTS 行,有什么变化吗?
  • 好吧,这只是导致它在没有选择器的情况下立即打开“文档”。然而,这让我有了一个有趣的发现。事实证明,“文档”实际上是一个非常方便的选择照片的界面,它确实可以正确过滤文件,所以我只能看到照片。所以它有效。但这不是很清楚。非技术用户可能不希望“文档”(带有文本文档图标)成为他们可以选择照片的地方。不知道我能做些什么,但我会玩弄它。感谢您的帮助!
  • 嗨 rnstewart,您是否设法让它与 LabeledIntent 一起工作?我也遇到这个。顺便说一句,它很好,例如Dropbox 或 Google Drive 也是此意图选择器中的一个选项。

标签: android image android-intent


【解决方案1】:

事实证明,“文档”实际上是选择照片的正确应用。它按预期的方式工作,只显示我画廊中的图像。名称和图标在这一点上并不完全清楚,我担心这可能会给技术含量较低的用户带来一些混淆。但我想我只需要解决这个问题。

【讨论】:

  • 您可以试试LabeledIntent,看看是否可以自定义标签和图标以满足您的需求。
  • 有趣的想法。我会调查的。非常感谢。
【解决方案2】:

看看这个:https://developer.android.com/guide/topics/providers/document-provider.html

Google 在 KitKat 中引入了 Storage Access Framework,它提供了一个通用的系统 UI 来选择文档而不是单个应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多