Android图库中的最大图像选择限制

10

我正在尝试从我的应用程序中获取图库内置应用程序中图像的Uri。

所以,我使用了下面的Intent,但它选择了更多的图像。

我想设置限制。少于3个。

@Override
public void onClick(View v) {
    Intent intent = new Intent( );
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    startActivityForResult(Intent.createChooser(intent, "select images"), PICK_IMAGE_MULTIPLE);
}

我该如何修复这个问题。

你有什么建议吗?

3个回答

12

1
一个没有解决方案的答案怎么可能有9分呢? - ekashking

1

正如Daniel所说,使用Intent.EXTRA_ALLOW_MULTIPLE是不可能的。然而,有一个替代方案,那就是使用MultipleImageSelect库。它不仅可以选择多张图片,还能够限制用户选择的图片数量。

请查看存储库样例

步骤:

步骤1: 像这样在你的build.gradle中添加MultipleImageSelect库和jitpack.io:

repositories {
  maven {
    url "https://jitpack.io"
  }
}

dependencies {
  implementation 'com.github.darsh2:MultipleImageSelect:v0.0.4'
}

第二步: 在项目的AndroidManifest.xml文件中,在application节点下添加以下内容:
<activity
  android:name="com.darsh.multipleimageselect.activities.AlbumSelectActivity"
  android:theme="@style/MultipleImageSelectTheme">
  <intent-filter>
     <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

第三步: 在您希望调用图像选择器的活动中,创建如下意图:
mSelectImagesBtn.setOnClickListener(view -> {
        Intent intent = new Intent(ListingImages.this, AlbumSelectActivity.class);
        intent.putExtra(Constants.INTENT_EXTRA_LIMIT, 3); //set desired image limit here
        startActivityForResult(intent, Constants.REQUEST_CODE);
    });

步骤4:并重写onActivityResult如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data !=null) {
        ArrayList<Image> images =data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
        imagePathList.clear();
       StringBuffer stringBuffer = new StringBuffer();
       
       //loop to retrieve the paths of each image and display to TextView
       for (int i = 0; i < images.size(); i++) {
            stringBuffer.append(images.get(i).path + "\n");
       }
        textView.setText(stringBuffer.toString());
    }
}

完成

或者,

如果您正在使用适配器来填充图像以显示,您可以改为这样:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
        ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
        imagePathList.clear();
        for (int i = 0; i < images.size(); i++) {
            imagePathList.add(images.get(i).path);
        }
        imageAdapter.notifyDataSetChanged();
    }
}

在ImageAdapter中,像以下这样显示图片以填充recyclerView:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    String path = imagePathList.get(position);
    Picasso.with(mContext)
            .load(new File(path))
            .placeholder(R.drawable.ic_house_placeholder)
            .into(holder.image);
}

0
为了对这个问题给出一个最新的答案,这是一个使用照片选择器的示例,它允许你限制最大选择的元素数量:
在你的活动中,将这个启动器声明为一个类字段。
    //In this example, the app lets the user select up to 5 media files.
private val galleryLauncher = registerForActivityResult(ActivityResultContracts.PickMultipleVisualMedia(5)) { uris ->
        if (!CollectionUtils.isEmpty(uris)) {
            //Do what you wanna do with your uri list
        }else {
            //nothing selected
        }
    }

只包含以下其中一种launch()调用,取决于您想让用户选择的媒体类型。
// Include only one of the following calls to launch(), depending on the types
// of media that you want to let the user choose from.

// Launch the photo picker and let the user choose images and videos.
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.ImageAndVideo))

// Launch the photo picker and let the user choose only images.
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))

// Launch the photo picker and let the user choose only videos.
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.VideoOnly))

// Launch the photo picker and let the user choose only images/videos of a
// specific MIME type, such as GIFs.
val mimeType = "image/gif"
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.SingleMimeType(mimeType)))

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