ContentResolver query doesn't return the newly inserted video
Asked Answered
D

0

0

I have inserted new video in MediaStore like this,

val resolver = requireActivity().applicationContext.contentResolver
val videoCollection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val newVideoDetails = ContentValues().apply {
    put(MediaStore.Video.Media.DISPLAY_NAME, file.name)
    put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000)
    put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis())
    put(MediaStore.Video.Media.IS_PENDING, 1)
    put(MediaStore.Video.Media.TITLE, file.name)
    put(MediaStore.Video.Media.RELATIVE_PATH, relativePath)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        put(MediaStore.Video.VideoColumns.DURATION, fileDuration)
    }
}
val newVideoUri = resolver
        .insert(videoCollection, newVideoDetails)
newVideoUri?.let {
    val inputStream = FileInputStream(file)
    val outputStream = resolver.openOutputStream(newVideoUri)
    val buffer = ByteArray(4096)
    var len: Int
    while (inputStream.read(buffer).also { len = it } != -1) {
        outputStream?.write(buffer, 0, len)
    }
    outputStream?.flush()
    inputStream.close()
    outputStream?.close()
}
newVideoDetails.clear()
newVideoDetails.put(MediaStore.Video.Media.IS_PENDING, 0)
newVideoUri?.let { it1 ->
    resolver.update(it1, newVideoDetails, null, null)
}

This works well and Video available in My Files app under Videos, But while I query using following code, contentresolver dosen't return this video.

val projection = arrayOf(
        MediaStore.Video.Media._ID,
        MediaStore.Video.Media.DISPLAY_NAME,
        MediaStore.Video.Media.DATE_TAKEN,
        MediaStore.Video.Media.DURATION,
        MediaStore.Video.Media.SIZE/*,
        MediaStore.Video.Media.ORIENTATION*/
)
val selection = "${MediaStore.Video.Media.DATE_TAKEN} >= ?"
val selectionArgs = arrayOf(
        // Release day of the G1. :)
        dateToTimestamp(day = 22, month = 10, year = 2008).toString()
)
val sortOrder = "${MediaStore.Video.Media.DATE_TAKEN} DESC"
val cursor = getApplication<Application>().contentResolver.query(
        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        selectionArgs,
        sortOrder
)

UPDATE While write using FileDescriptor I can query but returns empty file.

newVideoUri?.let {
    resolver.openFileDescriptor(newVideoUri, "w", null).use { descriptor ->
        {
            descriptor?.let {
                FileOutputStream(descriptor.fileDescriptor).use { out ->
                    FileInputStream(file).use { inputStream ->
                        val buffer = ByteArray(4096)
                        var len: Int
                        while (inputStream.read(buffer).also { len = it } != -1) {
                            out.write(buffer, 0, len)
                        }
                        out.flush()
                        inputStream.close()
                        out.close()
                    }
                }
            }
        }
    }
    /*val inputStream = FileInputStream(file)
    val outputStream = resolver.openOutputStream(newVideoUri)
    val buffer = ByteArray(4096)
    var len: Int
    while (inputStream.read(buffer).also { len = it } != -1) {
        outputStream?.write(buffer, 0, len)
    }
    outputStream?.flush()
    inputStream.close()
    outputStream?.close()*/
}
Disregard answered 3/8, 2020 at 1:34 Comment(3)
You query MediaStore.Video.Media.EXTERNAL_CONTENT_URI but your insert was to MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY).Poulson
Try without date taken.Poulson
No, It didn't work.Disregard

© 2022 - 2024 — McMap. All rights reserved.