[英]How to limit multiple image selection from the gallery?
I have implemented adding multiple image selection from the gallery in my project.我已经在我的项目中实现了从图库中添加多个图像选择。 However, I want to limit the user so he/she can select only 3 images from the gallery.但是,我想限制用户,以便他/她只能从图库中选择 3 张图片。
I have implemented selecting multiple images from the gallery like this:我已经实现了从图库中选择多个图像,如下所示:
`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"), 1);`
How can I achieve this?我怎样才能做到这一点?
Thanks.谢谢。
You can get the count of ClipData
when selecting multiple images from and gallery and if that count is grater than 3 you can notify the user about it.从图库中选择多个图像时,您可以获得ClipData
的计数,如果该计数大于 3,您可以通知用户。
You can do something like this after selecting images from gallery:从图库中选择图像后,您可以执行以下操作:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK || resultCode != RESULT_CANCELED){
ClipData clipData = data.getClipData();
if(clipData.getItemCount() > 3){
//notify user here...
}
}
}
This is not possible.这不可能。 https://developer.android.com/reference/android/content/Intent.html#EXTRA_ALLOW_MULTIPLE https://developer.android.com/reference/android/content/Intent.html#EXTRA_ALLOW_MULTIPLE
You'll have to manually check the returned data to see if it's more than 10 items, and if so, show a Toast您必须手动检查返回的数据以查看它是否超过 10 个项目,如果是,则显示 Toast
put this in your build.gradle(app)把它放在你的 build.gradle(app) 中
compile 'com.github.esafirm.android-image-picker:imagepicker:1.5.0'
compile 'com.github.esafirm.android-image-picker:rximagepicker:1.5.0'
and this in your activity这在你的活动中
private void pickImage() {
ImagePicker.create(UploadPhotosActivity.this)
.showCamera(false)
.limit(3)
.imageTitle(getString(R.string.select_image))
.folderTitle(getString(R.string.folder))
.theme(R.style.ImagePickerTheme)
.start(RC_CODE_PICKER);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_CODE_PICKER) {
Log.d("===uploadPhoto", "gallery : " + data);
imagesList = (ArrayList<Image>) ImagePicker.getImages(data);
Intent intent = new Intent(UploadPhotosActivity.this, ImageCropperActivity.class);
intent.putExtra(ImageCropperActivity.EXTRA_VIEW_PORT_RATIO, imagesList);
startActivity(intent);
}
} else {
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.