从android图库中select多个图像

所以基本上我试图实现的是在Android中打开Gallery ,并让用户selectmultiple images 。 现在这个问题经常被问到,但是我不满意答案。 主要是因为我在IDE中find了一些有趣的东西(我稍后回来),因此我不想使用自定义适配器,而只使用了香草。

现在我select一个图像的代码是:

 Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1); 

现在SO和其他网站上的人们会告诉你,你有两个select:

1)不要使用ACTION_GET_CONTENT而是使用ACTION_SEND_MULTIPLE
这一个不行。 这是根据文件sending文件,而不是retrieving ,这正是它所做的。 当使用ACTION_SEND_MULTIPLE我有一个窗口打开我的设备,我必须select一个应用程序发送我的数据。 这不是我想要的,所以我想知道人们如何得到这个解决scheme实现了…我错过了什么?

2)实现一个custom Gallery 。 现在这是我最后一个select,我会考虑,因为我不是我正在寻找,因为我必须自己的样式,为什么你不能在香草画廊select多个图像?

必须有一个选项这个..现在有趣的事情我所发现的是这样的:
我在ACTION_GET_CONTENT的文档描述中发现了这个ACTION_GET_CONTENT

如果调用者可以处理多个返回的项目(用户执行多重select),那么它可以指定EXTRA_ALLOW_MULTIPLE来指示这一点。

这很有趣。 这里他们指的是用户可以select多个项目的用例。

后来他们在文档中说:

您可以使用EXTRA_ALLOW_MULTIPLE来允许用户select多个项目。

所以这是非常明显的权利? 这是我需要的。 但我的下面的问题是:我在哪里可以把这个EXTRA_ALLOW_MULTIPLE ? 可悲的是,我无法find这个不在开发者.android指南的地方 ,也不是INTENT类中定义的常量。

有人可以帮助我这个EXTRA_ALLOW_MULTIPLE

通过Intent.putExtra()方法在意图上设置EXTRA_ALLOW_MULTIPLE选项:

 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 

你上面的代码应该是这样的:

 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); 

注意: EXTRA_ALLOW_MULTIPLE选项仅在Android API 18和更高版本中可用。

在类中定义这些variables:

 int PICK_IMAGE_MULTIPLE = 1; String imageEncoded; List<String> imagesEncodedList; 

让我们假设点击一个button,它应该打开图库来select图像

  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"), PICK_IMAGE_MULTIPLE); 

那么你应该重写onActivityResult方法

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { try { // When an Image is picked if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK && null != data) { // Get the Image from data String[] filePathColumn = { MediaStore.Images.Media.DATA }; imagesEncodedList = new ArrayList<String>(); if(data.getData()!=null){ Uri mImageUri=data.getData(); // Get the cursor Cursor cursor = getContentResolver().query(mImageUri, filePathColumn, null, null, null); // Move to first row cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); imageEncoded = cursor.getString(columnIndex); cursor.close(); } else { if (data.getClipData() != null) { ClipData mClipData = data.getClipData(); ArrayList<Uri> mArrayUri = new ArrayList<Uri>(); for (int i = 0; i < mClipData.getItemCount(); i++) { ClipData.Item item = mClipData.getItemAt(i); Uri uri = item.getUri(); mArrayUri.add(uri); // Get the cursor Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null); // Move to first row cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); imageEncoded = cursor.getString(columnIndex); imagesEncodedList.add(imageEncoded); cursor.close(); } Log.v("LOG_TAG", "Selected Images" + mArrayUri.size()); } } } else { Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) .show(); } super.onActivityResult(requestCode, resultCode, data); } 

注意:画廊不给你select多个图像的能力,所以我们在这里打开所有的图像工作室,你可以从中select多个图像。 并且不要忘记将权限添加到您的清单

非常重要: getData(); 得到一个单一的形象,我已经把它存储在imageEncodedstring,如果用户select多图像,那么他们应该存储在列表

所以你必须检查哪一个是空的使用另一个

祝你有一个很好的尝试和其他人

我希望这个答案不迟。 由于图库小部件默认不支持多选,但您可以自定义接受多选图标的gridview。 另一种select是扩展图库视图并添加自己的代码以允许多个select。
这是简单的图书馆可以做到这一点: https : //github.com/luminousman/MultipleImagePick

更新
从@ ilsy的评论来看 ,这个库中的CustomGalleryActivity使用了manageQuery ,这已经被弃用了,所以它应该改成getContentResolver().query()cursor.close()

很多这些答案都有相似之处,但都缺lessonActivityResult最重要的部分,检查data.getClipData是否为null 之前检查data.getData

调用文件select器的代码:

 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); //allows any image file type. Change * to specific extension to limit it //**These following line is the important one! intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURES); //SELECT_PICTURES is simply a global int used to check the calling intent in onActivityResult 

获取所选图像的代码:

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == SELECT_PICTURES) { if(resultCode == Activity.RESULT_OK) { if(data.getClipData() != null) { int count = data.getClipData().getItemCount(); int currentItem = 0; while(currentItem < count) { Uri imageUri = data.getClipData().getItemAt(currentItem).getUri(); //do something with the image (save it to some directory or whatever you need to do with it here) currentItem = currentItem + 1; } } else if(data.getData() != null) { String imagePath = data.getData().getPath(); //do something with the image (save it to some directory or whatever you need to do with it here) } } } } 

请注意,Android的select器在某些设备上提供了照片和图库。 照片允许select多个图像。 画廊一次只允许一个。

嗨下面的代码工作正常。

  Cursor imagecursor1 = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy + " DESC"); this.imageUrls = new ArrayList<String>(); imageUrls.size(); for (int i = 0; i < imagecursor1.getCount(); i++) { imagecursor1.moveToPosition(i); int dataColumnIndex = imagecursor1 .getColumnIndex(MediaStore.Images.Media.DATA); imageUrls.add(imagecursor1.getString(dataColumnIndex)); } options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.stub_image) .showImageForEmptyUri(R.drawable.image_for_empty_url) .cacheInMemory().cacheOnDisc().build(); imageAdapter = new ImageAdapter(this, imageUrls); gridView = (GridView) findViewById(R.id.PhoneImageGrid); gridView.setAdapter(imageAdapter); 

你想要更多的澄清。 http://mylearnandroid.blogspot.in/2014/02/multiple-choose-custom-gallery.html

我也有同样的问题。 我也想让用户在从画廊中挑选照片的同时轻松拍照。 无法find一个本地的做法,所以我决定做一个开源项目。 这很像MultipleImagePick,但只是更好的实现方法。

https://github.com/giljulio/android-multiple-image-picker

 private static final RESULT_CODE_PICKER_IMAGES = 9000; Intent intent = new Intent(this, SmartImagePicker.class); startActivityForResult(intent, RESULT_CODE_PICKER_IMAGES); @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case RESULT_CODE_PICKER_IMAGES: if(resultCode == Activity.RESULT_OK){ Parcelable[] parcelableUris = data.getParcelableArrayExtra(ImagePickerActivity.TAG_IMAGE_URI); //Java doesn't allow array casting, this is a little hack Uri[] uris = new Uri[parcelableUris.length]; System.arraycopy(parcelableUris, 0, uris, 0, parcelableUris.length); //Do something with the uris array } break; default: super.onActivityResult(requestCode, resultCode, data); break; } } 

我已经实现了一个相同的库(类似于其他这样的库)。 它还提供了一个专辑select器,可以从中挑选出所选专辑中的图像。 这里是链接: https : //github.com/dasnicdev/MultipleImagePick

试试这个IntentChooser 。 只需添加一些代码行,我为你做了其余的。

 private void startImageChooserActivity() { Intent intent = ImageChooserMaker.newChooser(MainActivity.this) .add(new ImageChooser(true)) .create("Select Image"); startActivityForResult(intent, REQUEST_IMAGE_CHOOSER); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE_CHOOSER && resultCode == RESULT_OK) { List<Uri> imageUris = ImageChooserMaker.getPickMultipleImageResultUris(this, data); } } 

PS:正如上面的答案所述,EXTRA_ALLOW_MULTIPLE仅适用于API> = 18.有些图库应用程序不能使此function可用(Google照片和文档( com.android.documentsui )工作。

智能android图库与多个图像select行动。

在这里检查演示

首先制作button进行操作,您可以将其用于单个/多个

对于单个图像select : – – luminous.ACTION_PICKselect单个图像。

对于多个图像select : – – luminous.ACTION_MULTIPLE_PICKselect多个图像。

在Android geekonjava中选择多个图像

MainActivity.java

 // For single image Intent i = new Intent(Action.ACTION_PICK); startActivityForResult(i, 100); // For multiple images Intent i = new Intent(Action.ACTION_MULTIPLE_PICK); startActivityForResult(i, 200); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 100 && resultCode == Activity.RESULT_OK) { adapter.clear(); viewSwitcher.setDisplayedChild(1); String single_path = data.getStringExtra("single_path"); imageLoader.displayImage("file://" + single_path, imgSinglePick); } else if (requestCode == 200 && resultCode == Activity.RESULT_OK) { String[] all_path = data.getStringArrayExtra("all_path"); ArrayList<CustomGallery> dataT = new ArrayList<CustomGallery>(); for (String string : all_path) { CustomGallery item = new CustomGallery(); item.sdcardPath = string; dataT.add(item); } viewSwitcher.setDisplayedChild(0); adapter.addAll(dataT); } } 

在AndroidManifest.xml中

 <activity android:name="CustomGalleryActivity" > <intent-filter> <action android:name="luminous.ACTION_PICK" /> <action android:name="luminous.ACTION_MULTIPLE_PICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>