Created
December 26, 2021 10:13
-
-
Save Varsha-Kulkarni/78c009cf1a90a9204e5c72c5e60944ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override suspend fun getVideos(): List<Video> = withContext(ioDispatcher) { | |
val videos = mutableListOf<Video>() | |
val uri: Uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI | |
// Display videos in alphabetical order based on their display name. | |
val sortOrder = "${MediaStore.Video.Media.DISPLAY_NAME} ASC" | |
val cursor: Cursor? = contentResolver.query(uri, null, null, null, sortOrder) | |
//looping through all rows and adding to list | |
if (cursor != null && cursor.moveToFirst()) { | |
do { | |
val id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)) | |
val title: String = | |
cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME)) | |
val duration: Int = | |
cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media.DURATION)) | |
val size = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)) | |
val video = Video( | |
ContentUris.withAppendedId( | |
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, | |
id | |
), title, duration, size | |
) | |
videos.add(video) | |
} while (cursor.moveToNext()) | |
} | |
cursor?.close() | |
return@withContext videos | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment