How to select multiple images from gallery in android?

前端 未结 9 1771
無奈伤痛
無奈伤痛 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:08

    Here is the code for Select Multiple Image and video from Default Gallery.

    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
    
                Intent i = new Intent();
                i.setType("image/*");
           //i.setType("video/*");
                i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                i.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(i, "android.intent.action.SEND_MULTIPLE"), 1);
            }
        });
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        System.out.println("++data" + data.getClipData().getItemCount());// Get count of image here.
    
            System.out.println("++count" + data.getClipData().getItemCount());
            Uri selectedImage = data.getClipData().getItemAt(0).getUri();//As of now use static position 0 use as per itemcount.
            Bitmap bitmap = null;
      //        Uri selectedImage1 = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("+++ clipdate" + selectedImage);
    
            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(bitmap);
        //        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 15:11

    There is EXTRA_ALLOW_MULTIPLE option is set on the intent through the Intent.putExtra() method:

      intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    

    In your code write as below:

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

    Note: the EXTRA_ALLOW_MULTIPLE option is only available in Android API 18 and higher.

    If you want to develop your own gallery then check out the Select Multiple Images from Gallery

    0 讨论(0)
  • 2020-11-27 15:12

    I think android does not support selecting multiple images. For that need to use some library. i was also stuck in the same

    https://github.com/luminousman/MultipleImagePick

    this one helped me.

    0 讨论(0)
  • 2020-11-27 15:15

    Complete Working Answer

    Intent

    Intent intent = new Intent();
    intent.setType("image/*");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    }
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_MULTIPLE);
    
    

    Activity Result

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            try {
                // When an Image is picked
                if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK
                        && null != data && null != data.getClipData()) {
        
                    ClipData mClipData = data.getClipData();
        
                    Toast.makeText(getActivity(), "You picked " +
                                    (mClipData.getItemCount() > 1 ? mClipData.getItemCount() + "Images" :
                                            mClipData.getItemCount() + "Image"),
                            Toast.LENGTH_LONG).show();
        
                    pickedImageContainer.removeAllViews();
        
                    int pickedImageCount;
        
                    for (pickedImageCount = 0; pickedImageCount < mClipData.getItemCount();
                         pickedImageCount++) {
        
                        ImageView productImageView =
                                new ImageView(getActivity());
        
                        productImageView.setAdjustViewBounds(true);
        
                        productImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        
                        productImageView.setLayoutParams(new LinearLayout
                                .LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.MATCH_PARENT));
        
                        pickedImageContainer.addView(productImageView);
        
                        Glide.with(getActivity())
                                .load(mClipData.getItemAt(pickedImageCount).getUri())
                                .fitCenter().placeholder(R.drawable.map_default)
                                .error(R.drawable.map_default)
                                .into(productImageView);
                    }
                } else {
                    Toast.makeText(getActivity(), "You haven't picked any Image",
                            Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                Toast.makeText(getActivity(), "Error: Something went wrong " + e.getMessage(), Toast.LENGTH_LONG)
                        .show();
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    
    0 讨论(0)
  • 2020-11-27 15:15
    This is manifest file code for app to get consider you are sending images to your app
    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.appshare"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="21" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                     <action
                    android:name="android.intent.action.SEND_MULTIPLE" />
                     <action
                    android:name="android.intent.action.SEND" />
                <category
                    android:name="android.intent.category.DEFAULT" />
                <data
                    android:mimeType="image/*" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
    Program code...
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Log.d("MainActivity", "on Create ");
    
            if (Intent.ACTION_SEND_MULTIPLE.equals(getIntent().getAction())&& getIntent().hasExtra(Intent.EXTRA_STREAM))
            {
                    ArrayList<Parcelable> list =
                            getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);
                                for (Parcelable parcel : list) {
                                   Uri uri1 = (Uri) parcel;
                                   Log.d("MainActivity", "on Create uri1 " + uri1);
                                   String sourcepath=getPath(uri1);
                                   Log.d("MainActivity", "on Create sourcepath " + sourcepath);
                                   /// do things here with each image source path.
                               }
                                //Commented by Aishwary coz I want to open the app after selecting images otherwise uncomment to just select 
                                //finish();
                }else{
                    // This part is of single image by Aishwary
                    Uri imageUri =(Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
                    Log.d("MainActivity", "on Create ImageUri " + imageUri);
                }
                }
    
        public  String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            startManagingCursor(cursor);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
            }
    
        }
    
    This will allow you to get source path and images url . Now you can make array for url and use this url as you want...
    
    0 讨论(0)
  • 2020-11-27 15:18
    int SELECT_PICTURE=1;
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_PICTURE);
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            switch (requestCode) {
                case SELECT_PICTURE:
                    Uri selectedImageUri = data.getData();
                    imgPerview.setBackgroundColor(Color.TRANSPARENT);
                    LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                    imgPerview.setLayoutParams(vp);
                    imgPerview.setScaleType(ImageView.ScaleType.FIT_XY);
                    imgPerview.setImageURI(selectedImageUri);
                    uri = selectedImageUri;
            }
        }
        catch (Exception ex)
        {}
    }
    
    0 讨论(0)
提交回复
热议问题