2015-06-23 17 views
8

ben kullanan Android galeriden birden fazla görüntü açmak istediğiniz toplama verir "Intent.EXTRA_ALLOW_MULTIPLE" amaç filtresi:Android: Intent.EXTRA_ALLOW_MULTIPLE sadece tek

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 
    final Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("image/*"); 
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
    intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); 
    startActivityForResult(Intent.createChooser(intent, "Add images"), SELECT_MULTIPLE_IMAGES); 
} 

Ama Normalde kullandığınız uygulaması (yerli galeri, QuickPic uygulaması) , Sadece tek bir resmi seçebilirim. Test cihazı Android 5.1 çalıştırıyor.

Birden çok görüntüyü nasıl seçebilirim?

+1

'EXTRA_ALLOW_MULTIPLE' herhangi' Intent' ekstralar ile olduğu gibi, bir istek değil, bir komuttur. Başladığınız faaliyetin onurlandırılmasına gerek yoktur. – CommonsWare

+0

'' EXTRA_ALLOW_MULTIPLE'' parametresini kullanarak çoklu görüntü çekmeyi destekleyen bir galeri uygulaması şansını biliyor musunuz? – Hyndrix

+0

Başımın üstünden çıkma, üzgünüm. – CommonsWare

cevap

11

Bu, şu anda 4.4 ve için Gallary kullanarak görüntü seçimi kapsar benim son canlı uygulama alanlarından biri olan çalışıyor Kendi özel galerinizi yazmanın üstünde ve altında.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
    try { 
     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"), SELECT_IMAGE_REQUEST_GALLERY); 
    }catch(Exception e){ 
     Intent photoPickerIntent = new Intent(this, XYZ.class); 
     startActivityForResult(photoPickerIntent, SELECT_IMAGE_REQUEST); 
    } 
} else { 
    Intent photoPickerIntent = new Intent(this, XYZ.class); 
    startActivityForResult(photoPickerIntent, SELECT_IMAGE_REQUEST); 
} 
+0

Görüntüleri seçmek için hangi uygulamayı kullanıyorsunuz? QuickPic ve Samsung Galeri uygulamasını denedim ancak her ikisi de yalnızca tekli toplama özelliğini destekliyor. Kodun benimkine çok benziyor. – Hyndrix

+0

Yukarıda Moto G ve Nexus 4, Nexus 5 ve Nexus 6 cihazlarında test ettim. – strike

+0

Sistem galerisi uygulamasıyla mı? – Hyndrix

0
/** 
* Extra used to indicate that an intent can allow the user to select and 
* return multiple items. 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. When 
* this happens, they should be returned as the {@link #getClipData()} part 
* of the result Intent. 
* 
* @see #ACTION_GET_CONTENT 
* @see #ACTION_OPEN_DOCUMENT 
*/ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (data.getData() != null) { 
      try { 
       files.clear(); 
       Uri uri = data.getData(); 
       String url = FileUtils2.getPath(this, uri); 
       assert url != null; 
       File file = new File(url); 
       files.add(file); 
       mPresenter.postAnnexData(files); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } else { 
      //If uploaded with the new Android Photos gallery 
      ClipData clipData = data.getClipData(); 
      files.clear(); 
      if (clipData != null) { 
       for (int i = 0; i < clipData.getItemCount(); i++) { 
        ClipData.Item item = clipData.getItemAt(i); 
        Uri uri = item.getUri(); 
        String url = FileUtils2.getPath(this, uri); 
        assert url != null; 
        File file = new File(url); 
        files.add(file); 
       } 
      } 
      mPresenter.postAnnexData(files); 
     } 
    } 
} 
İlgili konular