安卓:Intent.EXTRA_ALLOW_MULTIPLE只允许单选。

18

我想使用"Intent.EXTRA_ALLOW_MULTIPLE"意图过滤器从Android相册中打开多个图像:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    startActivityForResult(Intent.createChooser(intent, "Add images"), SELECT_MULTIPLE_IMAGES);
}

无论我使用哪个应用程序(原生相册,QuickPic应用程序),我只能选择一张图片。测试设备正在运行Android 5.1。

我该如何选择多张图片?

But 无论我使用哪个应用程序(原生相册,QuickPic应用程序),我只能选择一张图片。测试设备正在运行Android 5.1。

我该如何选择多张图片?


1
“EXTRA_ALLOW_MULTIPLE”是一个请求,而不是命令,就像任何“Intent”额外功能一样。没有要求启动的活动必须遵守它们。 - CommonsWare
1
你是否知道有哪个画廊应用支持使用“EXTRA_ALLOW_MULTIPLE”参数进行多图选择? - Hyndrix
抱歉,我脑海中暂时想不起来。 - CommonsWare
添加EXTRA_ALLOW_MULTIPLE对我可行,并允许我选择多个图像,但是我不知道如何在onActivityResult中获取URL。 - Tom Kincaid
http://www.javacodegeeks.com/2012/10/android-select-multiple-photos-from-gallery.html - Shubham AgaRwal
2
需要长按才能……不知道为什么这是默认行为。 - Nicholas Ng
4个回答

13

这是我最近一项实时应用中的功能,它可以在4.4及以上版本中使用Gallary选择图像,在4.4以下版本中则需要编写自己的自定义图库。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    try {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_IMAGE_REQUEST_GALLERY);
    }catch(Exception e){
        Intent photoPickerIntent = new Intent(this, XYZ.class);
        startActivityForResult(photoPickerIntent, SELECT_IMAGE_REQUEST);
    }
} else {
    Intent photoPickerIntent = new Intent(this, XYZ.class);
    startActivityForResult(photoPickerIntent, SELECT_IMAGE_REQUEST);
}

你用什么应用程序(例如)来选择图像?我已经尝试过QuickPic和三星相册应用程序,但似乎都只支持单个选择。你的代码看起来与我的非常相似。 - Hyndrix
我已在Moto G和Nexus 4、Nexus 5以及Nexus 6设备上进行了测试。 - strike
使用系统的相册应用? - Hyndrix
6
需要长按才能激活多选模式。 - Nicholas Ng
似乎在运行Android 9 API 27的Pixel XL上无法工作...我收到“没有应用程序可以执行此操作”的消息。 - mparkes
显示剩余2条评论

1
/**
 * Extra used to indicate that an intent can allow the user to select and
 * return multiple items. This is a boolean extra; the default is false. If
 * true, an implementation is allowed to present the user with a UI where
 * they can pick multiple items that are all returned to the caller. When
 * this happens, they should be returned as the {@link #getClipData()} part
 * of the result Intent.
 *
 * @see #ACTION_GET_CONTENT
 * @see #ACTION_OPEN_DOCUMENT
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (data.getData() != null) {
            try {
                files.clear();
                Uri uri = data.getData();
                String url = FileUtils2.getPath(this, uri);
                assert url != null;
                File file = new File(url);
                files.add(file);
                mPresenter.postAnnexData(files);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            //If uploaded with the new Android Photos gallery
            ClipData clipData = data.getClipData();
            files.clear();
            if (clipData != null) {
                for (int i = 0; i < clipData.getItemCount(); i++) {
                    ClipData.Item item = clipData.getItemAt(i);
                    Uri uri = item.getUri();
                    String url = FileUtils2.getPath(this, uri);
                    assert url != null;
                    File file = new File(url);
                    files.add(file);
                }
            }
            mPresenter.postAnnexData(files);
        }
    }
}

当我选择2或3张图片时,我只能读取第一张,因为data.getData始终不为空。 - shurrok

1

替代

startActivityForResult(Intent.createChooser(intent, "Add images"), SELECT_MULTIPLE_IMAGES);

尝试

startActivityForResult(intent, SELECT_MULTIPLE_IMAGES); (Deprecated)

或者

someActivityResultLauncher.launch(intent);

避免使用

Intent.createChooser

0

2
谢谢提供链接。还有很多其他的自定义解决方案可供选择。但我想使用“EXTRA_ALLOW_MULTIPLE”。 - Hyndrix
2019年这种情况仍然存在吗? - hippietrail

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接