【问题标题】:Picking multiple images on clicking from gallery从图库中单击时选择多个图像
【发布时间】:2018-08-10 06:14:51
【问题描述】:

我是 android 新手,我编写了代码来从图库中选择多个图像,为了实现这一点,我编写了 2 种方法。 在onActivityResult 上,我使用data.getClipdatarecieve 每个图像。

这是选择多张图片的第一种方式。

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

这是选择多张图片的第二种方式。

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

我选择了第二种选择多张图片的方式,因为在实现这一点时,它看起来更适合我的应用程序,我可以在单击时选择多张图片并停止使用第一种方式,因为我必须使用长按选择多张图片。事情进展顺利,就像在 android studio 模拟器和诺基亚设备 USB 调试上一样。但在一台三星设备上,我根本无法选择图像,而第一种方法在这里奏效。所以我的问题是如果可能的话,如何在这种情况下使用第二种方式来实现多个图像,并且这个问题也会出现在其他设备上吗?

【问题讨论】:

  • 我建议最好使用库Matisse
  • 是的图书馆好多了

标签: android image android-intent android-gallery


【解决方案1】:

正如您提到的三星选项 1 和其他设备选项 2 的工作。 所以只需设置条件并检查当时使用的是哪个设备

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

【讨论】:

  • @Danial 有什么反馈吗?
  • 是的,您的代码有帮助,但在选择多个图像时存在一些问题,因为它发送的是请求而不是命令,所以我最好使用库。
  • Arbaz 我的代码运行良好,我说的是意图。extra_allow_multiple 是一个请求,因此某些设备会拒绝多选图像,这就是我选择库的原因。我没有其他问题。
  • 很抱歉,实际上我以为您遇到了问题。
【解决方案2】:

我尝试了下面的代码,它可以工作。不要检查设备“型号”,而是检查制造商。这将重定向应用程序以打开文档选择器(而不是默认的图库应用程序),从那里您可以从菜单导航到“照片”。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多