如何分别从android画廊中选择多个图像

湿婆辛格

我要完成的工作是,每当我单击单独的Imageview时,都要选择一个清晰的图像。例如,如果单击ImageView_1,则可以从库中选择一张图像;如果单击Imagview_2,则可以从库中选择另一张图像。我已经看到这个问题的答案已经很多,但是它们都与我想做的不同。在先前的答案中,他们将所有图像作为列表显示在OnActivity结果中,并从图库中一次选择所有图像。

我的密码

使用的依赖

实现'com.theartofdev.edmodo:android-image-cropper:2.8。+'

 private void ImageOnclick(){
    
            image_profile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ContextCompat.checkSelfPermission(Upload_New_Product.this,
                            Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                        openFileChooser();
                    } else {
                        requestStoragePermission();
                    }
                }
            });
    
            image_profile2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if (ContextCompat.checkSelfPermission(Upload_New_Product.this,
                            Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                        openFileChooser2();
                    } else {
                        requestStoragePermission();
                    }
    
                }
            });
    
    
    
        }
    
        private void openFileChooser() {
    
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, PICK_IMAGE_REQUEST);
    
    
        }
    
        private void openFileChooser2() {
    
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, PICK_IMAGE_REQUEST2);
    
        }
    
        @RequiresApi(api = Build.VERSION_CODES.P)
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
    
            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
                ImageUri = data.getData();
    
                CropImage.activity(ImageUri)
                        .setGuidelines(CropImageView.Guidelines.ON)
                        //         .setAspectRatio(1, 1)
                        .start(this);
    
            } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
    
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                if (resultCode == RESULT_OK) {
                    resultUri = result.getUri();
                    if (Build.VERSION.SDK_INT >= 29) {
                        try {
                            bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), resultUri));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        // Use older version
                        try {
                            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), resultUri);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                
                    //setImage_profile();
                    resized = Bitmap.createScaledBitmap(bitmap, 600, 600, true);
                    image_profile.setImageBitmap(resized);
                
                } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                    Exception error = result.getError();
                }
    
            }
    
    
            //  UploadingImage();
            //  UploadingThumbnailImage();
    
    
    
            if (requestCode == PICK_IMAGE_REQUEST2 && resultCode == RESULT_OK && data != null) {
                ImageUri2 = data.getData();
    
                CropImage.activity(ImageUri2)
                        .setGuidelines(CropImageView.Guidelines.ON)
                        //         .setAspectRatio(1, 1)
                        .start(this);
    
            } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
    
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
    
                if (resultCode == RESULT_OK) {
                    resultUri2 = result.getUri();
    
    
                    if (Build.VERSION.SDK_INT >= 29) {
                        try {
                            bitmap2 = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), resultUri2));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        // Use older version
                        try {
                            bitmap2 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), resultUri2);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
    
                    resized2 = Bitmap.createScaledBitmap(bitmap2, 600, 600, true);
                    image_profile2.setImageBitmap(resized2);
                    //setImage_profile2();
                    //  UploadingImage();
    
                } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                    Exception error = result.getError();
                }
    
            }
    }
库冯切贝克·雅库波夫

您错过了这一行:

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

活动结果应如下所示:

   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PICK_IMAGE_REQUEST) {
            if(resultCode == Activity.RESULT_OK) {
                if(data.getClipData() != null) {
                    int count = data.getClipData().getItemCount();

                    for(int i = 0; i < count; i++)
                    Uri imageUri = data.getClipData().getItemAt(i).getUri();
                    //TODO: do something; here is your selected images
                }
            } else if(data.getData() != null) {
                String imagePath = data.getData().getPath();
                //TODO: do something
            }
        }
    }

意图:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从钛金画廊中选择多个图像?

来自分类Dev

如何识别从相机胶卷中选择的图像?

来自分类Dev

如何分别从用户获取多个文件?

来自分类Dev

在onActivityResult中标识是从画廊中选择图像还是从视频中选择-Android

来自分类Dev

如何使用Nativescript从android画廊中选择图片?

来自分类Dev

如何在Android项目中选择多个图像/视频?

来自分类Dev

如何在Android项目中选择多个图像/视频?

来自分类Dev

如何从设备中选择多个图像?

来自分类常见问题

如何从UIImagePickerController中选择多个图像

来自分类Dev

如何从图库中选择多个图像?

来自分类Dev

如何限制从图库中选择多个图像?

来自分类Dev

如何从Android的库中选择多个文件?

来自分类Dev

从图库中选择多个图像

来自分类Dev

在Ionic中选择多个图像

来自分类Dev

如何在Android中选择和取消选择图像?

来自分类Dev

从Android选择多个图像,如何获取Uris?

来自分类Dev

如何从图像中选择颜色

来自分类Dev

如何使用图像 URI 从 android 画廊查看图像

来自分类Dev

Phonegap从图库/相机中选择多个图像

来自分类Dev

在imagepickercontroller iOS中选择多个图像

来自分类Dev

循环选择多个图像并使用jQuery分别显示它们

来自分类Dev

当源文件中有多个图像时,如何从源文件中选择特定图像?

来自分类Dev

如何从选择中选择多个段落

来自分类Dev

在iPhone中删除“从画廊中选择”选项?

来自分类Dev

如何在Android中以编程方式向画廊添加图像或以编程方式刷新画廊

来自分类Dev

Android Image Picker从图库中选择多个图像,最大限制为5

来自分类Dev

使用 ImagePicker 在 android 中选择多个图像时,离子应用程序关闭

来自分类Dev

如何从存储中选择多个图像并将其显示在Flutter和Flutter Web App中?

来自分类Dev

Android Studio-从图库中选择图像

Related 相关文章

  1. 1

    如何从钛金画廊中选择多个图像?

  2. 2

    如何识别从相机胶卷中选择的图像?

  3. 3

    如何分别从用户获取多个文件?

  4. 4

    在onActivityResult中标识是从画廊中选择图像还是从视频中选择-Android

  5. 5

    如何使用Nativescript从android画廊中选择图片?

  6. 6

    如何在Android项目中选择多个图像/视频?

  7. 7

    如何在Android项目中选择多个图像/视频?

  8. 8

    如何从设备中选择多个图像?

  9. 9

    如何从UIImagePickerController中选择多个图像

  10. 10

    如何从图库中选择多个图像?

  11. 11

    如何限制从图库中选择多个图像?

  12. 12

    如何从Android的库中选择多个文件?

  13. 13

    从图库中选择多个图像

  14. 14

    在Ionic中选择多个图像

  15. 15

    如何在Android中选择和取消选择图像?

  16. 16

    从Android选择多个图像,如何获取Uris?

  17. 17

    如何从图像中选择颜色

  18. 18

    如何使用图像 URI 从 android 画廊查看图像

  19. 19

    Phonegap从图库/相机中选择多个图像

  20. 20

    在imagepickercontroller iOS中选择多个图像

  21. 21

    循环选择多个图像并使用jQuery分别显示它们

  22. 22

    当源文件中有多个图像时,如何从源文件中选择特定图像?

  23. 23

    如何从选择中选择多个段落

  24. 24

    在iPhone中删除“从画廊中选择”选项?

  25. 25

    如何在Android中以编程方式向画廊添加图像或以编程方式刷新画廊

  26. 26

    Android Image Picker从图库中选择多个图像,最大限制为5

  27. 27

    使用 ImagePicker 在 android 中选择多个图像时,离子应用程序关闭

  28. 28

    如何从存储中选择多个图像并将其显示在Flutter和Flutter Web App中?

  29. 29

    Android Studio-从图库中选择图像

热门标签

归档