Skip to content

Instantly share code, notes, and snippets.

@Varsha-Kulkarni
Created December 26, 2021 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Varsha-Kulkarni/78c009cf1a90a9204e5c72c5e60944ee to your computer and use it in GitHub Desktop.
Save Varsha-Kulkarni/78c009cf1a90a9204e5c72c5e60944ee to your computer and use it in GitHub Desktop.
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