How to select multiple images from gallery in android?

前端 未结 9 1849
無奈伤痛
無奈伤痛 2020-11-27 14:48

I am making a project in which i want to select multiple photos from gallery and want to save that in imageview array. I am able to import single image and save at imageview

9条回答
  •  [愿得一人]
    2020-11-27 15:31

    You can use Uri of images to store their location in MainActivity.java

    ArrayList Images = new ArrayList<>();
    ...
    ...
    
    
    public void PickImagesIntent(){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGES);
    }
    
       
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PICK_IMAGES) {
                if (data.getClipData() != null) {
                    ClipData mClipData = data.getClipData();
                    for (int i = 0; i < mClipData.getItemCount(); i++) {
                        ClipData.Item item = mClipData.getItemAt(i);
                        Uri imageUri = item.getUri();
                        Images.add(imageUri);
                    }
                } else if (data.getData() != null) {
                    Uri imageUri = data.getData();
                    Images.add(uri);
                }
            }
        }
    }
    

    You can send it to another class(ViewImage.java) in form of string

    Intent ImageIntent = new Intent(this,ViewImage.class);
    ImageIntent.putExtra("imageUri",Images.get(i).toString());
    stratActivity(ImageIntent);
    

    To receive it in another class(ViewImage.java)

    Intent intent = getIntent();
    String imageUriString = intent.getStringExtra("imageUri");
    Uri uri = Uri.parse(imageUriSrting);
    

    To set it to ImageView

    ImageView imageView = (ImageView)findViewById(##id##);
    imageView.setImageURI(uri);
    

提交回复
热议问题