2015-06-23 11 views
8

Tôi muốn mở nhiều hình ảnh từ thư viện Android sử dụng "Intent.EXTRA_ALLOW_MULTIPLE" lọc ý:Android: Intent.EXTRA_ALLOW_MULTIPLE chỉ cho phép đơn chọn

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

Nhưng bất cứ điều gì ứng dụng tôi sử dụng (bộ sưu tập bản xứ, QuickPic ứng dụng) , Tôi chỉ có thể chọn một ảnh duy nhất. Thiết bị thử nghiệm đang chạy Android 5.1.

Tôi làm cách nào để chọn nhiều hình ảnh?

+1

'EXTRA_ALLOW_MULTIPLE' là một yêu cầu, không phải là một lệnh, như với bất kỳ' extras Intent'. Không có yêu cầu rằng hoạt động bạn đang bắt đầu tôn vinh họ. – CommonsWare

+0

Bạn có biết tình cờ một ứng dụng thư viện hỗ trợ việc chọn nhiều ảnh bằng cách sử dụng tham số '' EXTRA_ALLOW_MULTIPLE''? – Hyndrix

+0

Không rời khỏi đỉnh đầu của tôi, xin lỗi. – CommonsWare

Trả lời

11

này hiện đang làm việc tại một trong những ứng dụng đang chạy gần đây của tôi trong đó bao gồm lựa chọn các hình ảnh sử dụng Gallary cho 4.4 và ở trên và bên dưới bằng cách viết thư viện tùy chỉnh của riêng bạn.

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

Bạn đang sử dụng ứng dụng nào (ví dụ) để chọn hình ảnh? Tôi đã thử QuickPic và ứng dụng Thư viện Samsung nhưng cả hai dường như chỉ hỗ trợ chọn một lần. Mã của bạn trông rất giống với mã của tôi. – Hyndrix

+0

Tôi đã thử nghiệm ở trên trên các thiết bị Moto G và Nexus 4, Nexus 5 và Nexus 6. – strike

+0

Với ứng dụng thư viện hệ thống? – Hyndrix

2

Ther là không nhiều chọn trong bộ sưu tập có nguồn gốc, nhưng bạn có thể làm điều đó với thư viện này: https://github.com/luminousman/MultipleImagePick

+2

Cảm ơn bạn đã liên kết. Có rất nhiều giải pháp tùy chỉnh khác ngoài đó. Nhưng tôi muốn sử dụng '' EXTRA_ALLOW_MULTIPLE''. – 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); 
     } 
    } 
} 
Các vấn đề liên quan