2013-04-07 36 views
5

Trong dự án của tôi, tôi muốn mở một thư viện trên một nút bấm và sẽ có thể chọn hình ảnh hoặc video để có được con đường của họ.android- mở thư viện và chọn hình ảnh và video

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

startActivityForResult(i, RESULT_LOAD_IMAGE); 

Từ mã trên tôi có thể mở thư viện nhưng trong trường hợp này tôi chỉ có thể chọn hình ảnh. Vì vậy, hãy giúp tôi trong việc lựa chọn video cũng. Cảm ơn trước.

Trả lời

7

Dưới đây đang giải quyết vấn đề của tôi

final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT); 
         galleryIntent.setType("*/*"); 
         startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE); 
+2

đây là một giải pháp kém mà không lọc bất cứ điều gì, bạn có thể chọn một pdf ... – desgraci

+1

Thật vậy, bạn nên chấp nhận câu trả lời của KILA thay vì – akohout

14

Bạn có thể sử dụng đoạn mã tiếp theo:

Intent mediaChooser = new Intent(Intent.ACTION_GET_CONTENT); 
//comma-separated MIME types 
mediaChooser.setType("video/*, image/*"); 
startActivityForResult(mediaChooser, RESULT_LOAD_IMAGE); 

Nhưng tôi nghĩ rằng nó chỉ làm việc trên ICS hoặc lớn hơn

+5

phải là "hình ảnh/*" thay vì "hình ảnh/*" –

+6

trên 6.0 điều này chỉ cho phép video được chọn – Danedo

+0

Điều này dường như bị hỏng, ít nhất là cho các phiên bản mới hơn của ứng dụng ảnh android hoặc google. Có vẻ như chỉ có loại mime đầu tiên trong danh sách được phân cách bằng dấu phẩy được nhận dạng. – lostintranslation

2

Thay đổi Ý định của bạn để này :

Intent i = new Intent(Intent.ACTION_PICK, 
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI); 

Khi cố gắng tải video, bạn cần ghi rõ là mediaStore rằng video theo thứ tự chứ không phải hình ảnh như bạn đã viết.

9

Bạn cần sử dụng sau khi chọn Ý định

Intent photoLibraryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
photoLibraryIntent.setType("image/* video/*"); 
1

này là tốt nhất tôi biết ...... Hãy thử điều này cho một lần ....

final CharSequence[] options = {"Images", "Videos", "Cancel"}; 
      AlertDialog.Builder builder = new AlertDialog.Builder(OpenGallery.this); 
      builder.setTitle("Select From..."); 
      builder.setItems(options, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int item) { 
        if (options[item].equals("Images")) { 
         Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
         intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
         startActivityForResult(intent, 1); 
        } else if (options[item].equals("Videos")) { 
         Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI); 
         intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
         startActivityForResult(intent, 1); 
        } else if (options[item].equals("Cancel")) { 
         dialog.dismiss(); 
        } 
        dialog.dismiss(); 
       } 
      }); 
      builder.show(); 
5

Trên Android 6.0 và ở trên sử dụng loại "video/* hình ảnh/" hoặc "hình ảnh/ video/*" không hoạt động, nó chỉ nhận ra bộ lọc đầu tiên bạn chỉ định. Tôi đã giải quyết vấn đề bằng cách sử dụng mã này:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
photoPickerIntent.setType("*/*"); 
photoPickerIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"}); 
startActivityForResult(photoPickerIntent, Constants.SELECT_PHOTO); 

Mặc dù điều này sẽ hỏi người dùng ứng dụng họ muốn sử dụng để chọn hình ảnh/video.

+0

Cảm ơn, phản ứng này nên cao hơn ngày nay – Arcantos

Các vấn đề liên quan