[英]maximum image selection limit from gallery Android
I am trying to get image's Uri in the Gallery built-in app from inside my application. 我正在尝试从我的应用程序内部的Gallery内置应用程序中获取图像的Uri。
so, I was using the Intent below, but it selected many more image. 因此,我在下面使用Intent,但它选择了更多图像。
i want to set limitation. 我想设定限制。 less than 3 少于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);
}
how do i fix this. 我该如何解决。
Do you have any suggestions? 你有什么建议吗?
Unfortunately, as stated by http://developer.android.com/reference/android/content/Intent.html#EXTRA_ALLOW_MULTIPLE , this is not possible. 不幸的是,如http://developer.android.com/reference/android/content/Intent.html#EXTRA_ALLOW_MULTIPLE所述 ,这是不可能的。
This is a boolean extra; 这是布尔附加值; the default is false. 默认为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. 如果为true,则允许实现为用户提供一个UI,用户可以在其中选择多个都返回给调用者的项目。
You'll have to manually check the returned data to see if it's more than 3 items, and if so, show a Toast and let them try again. 您必须手动检查返回的数据以查看其是否超过3个项目,如果是,请显示Toast并让他们再试一次。
As Daniel stated, there is no possibility with Intent.EXTRA_ALLOW_MULTIPLE. 正如Daniel所说,Intent.EXTRA_ALLOW_MULTIPLE是不可能的。 An alternative though, is using the MultipleImageSelect library. 但是,一种替代方法是使用MultipleImageSelect库。 Not only can you select multiple images, but the extra ability to set a limit on images selected by the user. 您不仅可以选择多张图像,还可以对用户选择的图像设置限制。
Check out the repository or a sample . 签出存储库或样本 。
Step 1: Add MultipleImageSelect library, together with jitpack.io to your build.gradle like this: 步骤1:像这样,将MultipleImageSelect库和jitpack.io一起添加到build.gradle中:
repositories {
maven {
url "https://jitpack.io"
}
}
dependencies {
implementation 'com.github.darsh2:MultipleImageSelect:v0.0.4'
}
Step 2: In project's AndroidManifest.xml, add the following under application node: 步骤2:在项目的AndroidManifest.xml中,在应用程序节点下添加以下内容:
<activity
android:name="com.darsh.multipleimageselect.activities.AlbumSelectActivity"
android:theme="@style/MultipleImageSelectTheme">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Step 3: In the activity from where you want to call image selector, create Intent as follows: 步骤3:在您要从中调用图像选择器的活动中,按以下方式创建Intent:
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);
});
Step 4: and override onActivityResult like this: 步骤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());
}
}
DONE DONE
Alternatively, 或者,
If you're using an Adapter to inflate images to display, you can instead have this: 如果您使用适配器来放大要显示的图像,则可以使用以下方法:
@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();
}
}
Within ImageAdapter, display images to populate recyclerView, like follows: 在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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.