You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ClipData holds multiple URIs and it is generally useful if you are firing a chooser intent with ability to select multiple items. In those cases, the intent returned contains ClipData which in turn provides list of URIs of the selected items.
Previously we needed to write the boilerplate code to get list of URIs from ClipData and vice versa. Instead I thought to utilize the extension functions to make it simple and clean.
Below are my proposed functions to convert ClipData to list of URIs and the reverse from list of URIs to ClipData:
ClipData to List
fun ClipData.toList(): MutableList<Uri> {
val length = itemCount
val result = mutableListOf<Uri>()
for (i in 0 until length) {
result.add(getItemAt(i).uri)
}
return result
}
List to ClipData
fun List<Uri>.toClipData(label: String): ClipData {
return ClipData(
ClipDescription(label, arrayOf(ClipDescription.MIMETYPE_TEXT_URILIST)),
ClipData.Item(get(0))
).apply {
for (i in 1 until size) {
addItem(ClipData.Item(get(i)))
}
return this
}
}
ClipDataholds multiple URIs and it is generally useful if you are firing a chooser intent with ability to select multiple items. In those cases, the intent returned containsClipDatawhich in turn provides list of URIs of the selected items.Previously we needed to write the boilerplate code to get list of URIs from
ClipDataand vice versa. Instead I thought to utilize the extension functions to make it simple and clean.Below are my proposed functions to convert
ClipDatato list of URIs and the reverse from list of URIs toClipData:ClipData to List
List to ClipData
I'm open to suggestions, btw.