properly fetch duration from content uris

This commit is contained in:
tibbi 2020-05-06 11:28:46 +02:00
parent 5be5de3dac
commit b73bebf085

View file

@ -705,18 +705,18 @@ fun Context.getVideoResolution(path: String): Point? {
fun Context.getDuration(path: String): Int? { fun Context.getDuration(path: String): Int? {
val projection = arrayOf( val projection = arrayOf(
Video.Media.DURATION MediaColumns.DURATION
) )
val uri = Files.getContentUri("external") val uri = getFileUri(path)
val selection = "${Video.Media.DATA} = ?" val selection = if (path.startsWith("content://")) "${BaseColumns._ID} = ?" else "${MediaColumns.DATA} = ?"
val selectionArgs = arrayOf(path) val selectionArgs = if (path.startsWith("content://")) arrayOf(path.substringAfterLast("/")) else arrayOf(path)
try { try {
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null) val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use { cursor?.use {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
return Math.round(cursor.getIntValue(Video.Media.DURATION) / 1000.toDouble()).toInt() return Math.round(cursor.getIntValue(MediaColumns.DURATION) / 1000.toDouble()).toInt()
} }
} }
} catch (ignored: Exception) { } catch (ignored: Exception) {