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. 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.
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. An alternative though, is using the MultipleImageSelect library. Not only can you select multiple images, but the extra ability to set a limit on images selected by the user.
正如Daniel所说,使用Inent.EXTRA_ALLOW_MULTIPLE是不可能的。不过,另一种方法是使用MultipleImageSelect库。您不仅可以选择多个图像,还可以对用户选择的图像设置额外的限制。
Check out the repository or a sample.
查看存储库或样本。
STEPS:
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:
第二步:在项目的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:在您想要调用图像选择器的活动中,按如下方式创建意图:
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
干完
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中,显示图像以填充recumerView,如下所示:
@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);
}
To have an updated answer for this question, this is an example of how to use Photo Picker which allows you to limit the max selected elements :
为了获得此问题的最新答案,以下是如何使用照片拾取器的示例,它允许您限制最大选定元素:
declare this launcher as a class field in your activity
在您的活动中将此启动器声明为类字段
//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
}
}
Include only one of the following calls to launch(), depending on the types of media that you want to let the user choose from.
根据您要让用户从中选择的媒体类型,仅包含以下启动()调用之一。
// 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)))
更多回答
How can an answer with no solution have 9 points?
没有答案的答案怎么会有9分呢?
我是一名优秀的程序员,十分优秀!