Android: Intent.EXTRA_ALLOW_MULTIPLE 只允许单选

Android: Intent.EXTRA_ALLOW_MULTIPLE allows only single picking

我想使用 "Intent.EXTRA_ALLOW_MULTIPLE" intent 过滤器打开 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 应用程序),我只能 select 一张图片。测试设备为运行 Android 5.1.

如何选择多张图片?

本机库中没有 multi select,但您可以使用此库来实现: https://github.com/luminousman/MultipleImagePick

这目前正在我最近的一个实时应用程序中工作,该应用程序涵盖使用 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);
}
/**
 * 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);
        }
    }
}

而不是

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

尝试

startActivityForResult(intent, SELECT_MULTIPLE_IMAGES); (Deprecated)

或者

someActivityResultLauncher.launch(intent);

避免使用

Intent.createChooser