Reputation: 31
I am currently implementing a feature in Android where a user selects multiple media as part of the creation process of a Post. This media is then compressed and displayed in the order of selection by the user,
I was able to implement this in Android 12, but on testing with android 13, the media is arranged and returned in the order of images first, and the videos lost.
Does anyone have a work around to this, or is there a picker library that arranges media in order of selection and even displays the media with a number icon on top?
I am currently using the official Photo Picker library provided by google
https://developer.android.com/training/data-storage/shared/photopicker
Here is an example on how I am handling the returned images as well as the compression.
val multiplePhotoPickerLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickMultipleVisualMedia(5),
onResult = { data ->
if (data.isNotEmpty()) {
compressedVideos.clear()
selectedImage.apply {
addAll(data)
}
coroutineScope.launch {
for (i in data.indices) {
val isImage = getMediaType(
context, data[i]
) != MediaType.MediaTypeVideo
compressedVideos.add(
VideoItem(
data[i],
null,
false,
isImage,
"",
selectedMedias.size + i,
data[i].length(context)
)
)
Timber.tag("NoticeBoard").d("Item is Image ${isImage} is in postion ${i}")
}
val largeVideos = compressedVideos.filter { it.size > MAX_UPLOAD_SIZE }.map {
VideoItem(
it.currentPath,
it.currentPath,
it.isCompressed,
it.isImage,
it.itemId,
it.index,
it.size
)
}
compressedVideos.removeIf { it.size > MAX_UPLOAD_SIZE }
val compressor = MediaCompressor(context)
context.getActivity()?.let {
isCompressing.value = true
// function to compress videos
compressor.compressMultipleFiles(
it,
compressedVideos.filter { !it.isImage }.toMutableList(),
callback = { videosItems ->
//Compress Images
val images = compressImages(
compressedVideos.filter { it.isImage }.toMutableList(),
context
)
selectedMedias.addAll(images)
selectedMedias.addAll(videosItems)
selectedMedias.addAll(largeVideos)
selectedMedias.sortBy { it?.index }
isCompressing.value = false
}
)
}
}
}
}
)
Upvotes: 1
Views: 81