Multiple image select from gallery

后端 未结 1 1193
借酒劲吻你
借酒劲吻你 2020-12-22 08:43

I am a beginner at Android.

I want to select multiple images from gallery and view them to a horizontal scroll view. Currently I\'m able to select a single image and

相关标签:
1条回答
  • 2020-12-22 09:46

    Start activity to choose your image with this intent

    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"), UploadingHelper.REQUEST_CODE);
    

    then select any number of images you want and then in your activity result

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && intent.getClipData() != null) {
                int numberOfImages = intent.getClipData().getItemCount();
    for (int i = 0; i < numberOfImages; i++) {
    try {
          ImageData imageData = new ImageData();
          imageData.uri = intent.getClipData().getItemAt(i).getUri();
    
          Bitmap bitmap = getBitmap(intent.getClipData().getItemAt(i).getUri());
         imageData.icon = Bitmap.createScaledBitmap(bitmap, getPixelsFromDP(widthOfImages), getPixelsFromDP(widthOfImages), false);
    
          images.add(imageData);
          } catch (Exception e) {
                  Logger.log(e);
               }
          }
    
    }
    

    This is the Image Data class used.

    public class ImageData {
            public Bitmap icon;
            public Uri uri;
    }
    

    Sorry I forgot add the method

    private int getPixelsFromDP(float dp) {
            return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,  Resources.getSystem().getDisplayMetrics());
        }
    
    0 讨论(0)
提交回复
热议问题