I need something really simple - I have an mp4 file that my app recorded and I want to insert it to the MediaStore to the user's video collection. Here is my code, including the recommended ways of dealing with the new Scoped Storage:
// fileToExpose is in internal storage with a name like video_1.mp4
fun copyVideoFileToMediaStore(fileToExpose: File, context: Context): Boolean {
val resolver = context.contentResolver
val volume = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) MediaStore.VOLUME_EXTERNAL_PRIMARY else MediaStore.VOLUME_EXTERNAL
val videoCollection = MediaStore.Video.Media.getContentUri(volume)
val videoDetails = ContentValues().apply {
put(MediaStore.Video.Media.DISPLAY_NAME, fileToExpose.name)
put(MediaStore.Video.Media.MIME_TYPE, "video/mp4")
put(MediaStore.Video.Media.TITLE, name)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
put(MediaStore.Video.Media.IS_PENDING, 1)
}
}
val videoContentUri = resolver.insert(videoCollection, videoDetails) ?: return false
resolver.openFileDescriptor(videoContentUri, "w", null).use { pfd ->
pfd ?: return false
fileToExpose.inputStream().use { input ->
FileOutputStream(pfd.fileDescriptor).use { output ->
input.copyTo(output)
}
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
videoDetails.clear()
videoDetails.put(MediaStore.Video.Media.IS_PENDING, 0)
resolver.update(videoContentUri, videoDetails, null, null)
}
return true
}
However, after this function call the file with name video_1.mp4 becomes something like 1589077991588.3gp, so both the name and the extension are different (my device is Android 9). MediaStore.MediaColumns.DATA is deprecated. How can I fix this?