简体   繁体   English

从图库中单击时选择多个图像

[英]Picking multiple images on clicking from gallery

I am new to android and I have written code to select multiple images from gallery and for achieving this i have written 2 ways of doing this.我是 android 新手,我编写了代码来从图库中选择多个图像,为了实现这一点,我编写了 2 种方法。 On onActivityResult I am using data.getClipdata to recieve each image.onActivityResult我使用data.getClipdatarecieve每个图像。

This is the first way of selecting multiple images.这是选择多个图像的第一种方法。

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

This is the second way of selecting multiple images.这是选择多个图像的第二种方法。

Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent,RESULT_LOAD_IMAGE);

I went with the second way of selecting multiple images, because on implementing this it looked better for my app and I could select multiple images on just click and stopped using first way as I have to select multiple images using long press.我采用了第二种选择多张图片的方式,因为在实现这一点时,它对我的​​应用程序来说看起来更好,我只需单击即可选择多张图片并停止使用第一种方式,因为我必须使用长按来选择多张图片。 Thing were going fine as on android studio emulator and Nokia device USB debugging.在 android studio 模拟器和诺基亚设备 USB 调试上一切顺利。 But on one Samsung device I was not able to select images at all and here the first method worked.但是在一台三星设备上,我根本无法选择图像,这里第一种方法有效。 So my question is how to achieve multiple images in this scenario using second way if possible and will this issue on some other device too?所以我的问题是如果可能的话,如何在这种情况下使用第二种方式实现多个图像,并且这个问题也会出现在其他设备上吗?

As you mention in question for Samsung option 1 and for other device option 2 work.正如您提到的三星选项 1 和其他设备选项 2 的问题。 so just put condition and check which device is use at that time所以只需输入条件并检查当时正在使用哪个设备

String deviceName = "Samsung";
if(deviceName.e(android.os.Build.MODEL)){
    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"), RESULT_LOAD_IMAGE);
}else{
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    startActivityForResult(intent, RESULT_LOAD_IMAGE);
}

I tried the below code and it works.我尝试了下面的代码,它的工作原理。 Instead of checking the device "model", check for the Manufacturer.与其检查设备“型号”,不如检查制造商。 This will redirect the app to open the document chooser (instead of the default gallery app), from there you can navigate to "Photos" from the menu.这将重定向应用程序以打开文档选择器(而不是默认的图库应用程序),您可以从那里导航到菜单中的“照片”。

public void captureImageFromGallery() {
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

    String deviceName = "Samsung";
    if(deviceName.equalsIgnoreCase(Build.MANUFACTURER)) {
        intent.setDataAndType(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
    }
    startActivityForResult(intent, REQ_CODE_GALLERY);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM