Home > other >  how to select multiple images in android
how to select multiple images in android

Time:10-11

I am trying to allow select multiple images in my android application which works fine but for some reasons i am not able to get the data of the images selected in the activity result. The clipData seems to be empty when i select multiple images but the data works fine when i select a single image. Here is the code i currently have.

    private void selectImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                && data != null && data.getData() != null) {

            if (data.getClipData() != null) {
                ClipData mClipData = data.getClipData();
                Toast.makeText(getApplicationContext(), "" mClipData.getItemCount(), Toast.LENGTH_SHORT).show();
            } else if (data.getData() != null) {
                Toast.makeText(getApplicationContext(), "One Image", Toast.LENGTH_SHORT).show();
            }


        }
    }

For some reasons when i select one image i get the toast One Image but when i select multiple images no toast whereas i am expecting the clipdata to show number of items(images) selected.

CodePudding user response:

You should remove check "data.getData() != null", hope it's ok

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null) {

        if (data.getClipData() != null) {
            ClipData mClipData = data.getClipData();
            Toast.makeText(getApplicationContext(), "" mClipData.getItemCount(), Toast.LENGTH_SHORT).show();
        } else if (data.getData() != null) {
            Toast.makeText(getApplicationContext(), "One Image", Toast.LENGTH_SHORT).show();
        }


    }
}

CodePudding user response:

Try this code for multiple image selection. You can use Image Uri and Image Path it's up to your need is it path or uri.

  val PICK_IMAGE_MULTIPLE = 2
  private var pathList: ArrayList<String> = ArrayList()


override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val imagesSelectButton:Button = findViewById(R.id.btn_selectImages)
        imagesSelectButton.setOnClickListener {
            photoIntentMethod()
        }

    }

  override fun onActivityResult(requestCode: Int, resultCode: Int, intentData: Intent?) {
        super.onActivityResult(requestCode, resultCode, intentData)

        if (resultCode == RESULT_OK) {
            if (requestCode == PICK_IMAGE_MULTIPLE) {

                if (intentData!!.clipData != null) {
                    val mClipData = intentData.clipData
                    for (i in 0 until mClipData!!.itemCount) {
                        val item = mClipData.getItemAt(i)
                        val imageUri = item.uri
                        pathList.add(getImagePath(imageUri))
                    }
                } else if (intentData.data != null) {
                    val imageUri = intentData.data
                    pathList.add(getImagePath(imageUri!!))
                }
            }
        }

    }


    @SuppressLint("Range")
    private fun getImagePath(uri: Uri): String {
        var cursor: Cursor? = contentResolver.query(uri, null, null, null, null)
        cursor?.moveToFirst()
        var documentId: String = cursor!!.getString(0)
        documentId = documentId.substring(documentId.lastIndexOf(":")   1)
        cursor.close()
        cursor = contentResolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null, MediaStore.Images.Media._ID   " = ? ", arrayOf(documentId), null
        )
        cursor?.moveToFirst()
        val path: String = cursor!!.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))
        cursor.close()
        return path
    }

    private fun photoIntentMethod() {
        val intent = Intent()
        intent.type = "image/*"
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
        intent.action = Intent.ACTION_GET_CONTENT
        startActivityForResult(
            Intent.createChooser(intent, "Select Picture"),
            PICK_IMAGE_MULTIPLE
        )
    }
  • Related