span
span

Reputation: 5624

Select multiple media items with Intent.ACTION_PICK?

I'm writing an application that will allow the user to select multiple music tracks from the sd-card and play them. I'm trying to use the Intent system to select multiple media instances but I can only find a way to pick a single item.

This is the code I'm using:

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_MEDIA);

Since there is an action "ACTION_SEND_MULTIPLE" I thought there would be a multiple version of the pick action as well but I can't find any. So, is it possible and if so, what is the action to use?

Upvotes: 0

Views: 2708

Answers (2)

JacksOnF1re
JacksOnF1re

Reputation: 3512

In my opinion the selected answer is not (no longer) correct. It depends on the app that will be opened to select a file (image, video etc).

For example, if you have Google Photos installed, it is indeed possible to use Intent.ACTION_PICK for multiple images.

        val pickIntent = Intent(Intent.ACTION_PICK).apply {
        putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
        setType("image/*")
    }

This will open Google Photos and give the user the option to select multiple images on long click. The result of the Contract activity result will be clipData.

    override fun parseResult(resultCode: Int, intent: Intent?): List<Uri> {
    return intent?.takeIf { resultCode == RESULT_OK }?.getClipDataUris() ?: emptyList()
}

/**
 * @see ActivityResultContracts.GetMultipleContents
 */
private fun Intent.getClipDataUris(): List<Uri> {
    // Use a LinkedHashSet to maintain any ordering that may be
    // present in the ClipData
    val resultSet = LinkedHashSet<Uri>()
    data?.let { data ->
        resultSet.add(data)
    }
    val clipData = clipData
    if (clipData == null && resultSet.isEmpty()) {
        return emptyList()
    } else if (clipData != null) {
        for (i in 0 until clipData.itemCount) {
            val uri = clipData.getItemAt(i).uri
            if (uri != null) {
                resultSet.add(uri)
            }
        }
    }
    return ArrayList(resultSet)
}

Upvotes: 0

Royston Pinto
Royston Pinto

Reputation: 6721

I unfortunately faced the same problem. There is NO way to select multiple items with Intent.ACTION_PICK. The way i took was to create my own custom GridView for the user to select multiple items. You could take a similar approach of showing all the mp3 files in a customized view.

Upvotes: 2

Related Questions