2

我有一个意图,可用于允许用户选择图像应用程序中的某些图像,如厨房(或用户设备中的任何其他)。设置意图返回的项目的最大数量

我想让用户选择只有10张图片但我不知道如何设置这个最大意图。 我试图看看我是否可以使用ClipData,但clipdata没有方法来设置最大数量的项目。

ClipboardManager manager = getSystemService(Context.CLIPBOARD_SERVICE) 
ClipData clipdata = manager.getPrimaryClip();// in short whether i get 
or i create a clipdata, there are no methods to set maximum number of 
items to be held into that clip 

这是我的意图。

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
    photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
    photoPickerIntent.setType("image/*"); 
    startActivityForResult(photoPickerIntent, SELECT_PHOTO); 

我怎么能限制用户只能选择10张照片?

回答

1

使用ClipData检查返回的项目数

ClipData mClipData = data.getClipData(); 
// here you can check how many images user has selected. 
if(mClipData.getItemCount() >= 10) { 
    // do needful here 
    Log.e("APP_TAG", "Greater than THRESHOLD."); 
    // show some error 
    return; 
} 

参考this问题的更多细节。

0

在每次选择时使用循环,如果选择的图像大小大于10,则做一些事情。

2

不通过ACTION_PICK。 ACTION_PICK未被记录为完全支持EXTRA_ALLOW_MULTIPLE,因此可能有设备不允许针对该意图进行多重选择。即使在EXTRA_ALLOW_MULTIPLE是协议的一部分(例如ACTION_GET_CONTENT)的Intent操作中,也没有用于控制最大计数的附加项。

欢迎您创建自己的图像选择UI。 我们可以将这个库用于具有setSelectionLimit属性的多个图像选择器。 https://github.com/jaydeepw/poly-picker

private void getImages() { 
    Intent intent = new Intent(mContext, ImagePickerActivity.class); 
    Config config = new Config.Builder() 
      .setTabBackgroundColor(R.color.white) // set tab background color. Default white. 
      .setTabSelectionIndicatorColor(R.color.blue) 
      .setCameraButtonColor(R.color.green) 
      .setSelectionLimit(2) // set photo selection limit. Default unlimited selection. 
      .build(); 
    ImagePickerActivity.setConfig(config); 
    startActivityForResult(intent, INTENT_REQUEST_GET_IMAGES);} 
+0

它似乎只限于厨房应用程序。不是吗? –

+0

使用此库它会从设备的所有图像。我们可以限制选择图像。它不仅限于画廊应用 – Venkatesh

+0

我要测试它并告诉你结果。我看到这个库有很多问题,包括github上的内存问题。 –