Tuesday, 19 January 2021

Unable to retrieve latest downloaded Video using MediaStore

I am using DownloadManager to download a video in the Downloads directory. Once the video is downloaded, I query the download directory using MediaStore. The problem is that I am getting the video that was downloaded last to second instead of the latest one.

Video Download Code

val request = DownloadManager.Request(Uri.parse(downloadUrl))
                .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
                .setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS,
                    "MyApp/CategoryOne/123.mp4"
                )
                .setTitle(fileName)
                .setDescription(context.getString(R.string.all_text_downloading))

            val downloadManager =
                context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
            val downloadID = downloadManager.enqueue(request)

            var isDownloadFinished = false
            var downloadProgress: Int
            while (!isDownloadFinished) {
                val cursor: Cursor =
                    downloadManager.query(DownloadManager.Query().setFilterById(downloadID))
                if (cursor.moveToFirst()) {
                    when (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
                        DownloadManager.STATUS_FAILED -> {
                            isDownloadFinished = true
                            emit(State.error(context.getString(R.string.all_error_download_failed)))
                        }
                        DownloadManager.STATUS_PAUSED -> {
                        }
                        DownloadManager.STATUS_PENDING -> {
                        }
                        DownloadManager.STATUS_RUNNING -> {
                            val totalSizeInBytes: Long =
                                cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))
                            if (totalSizeInBytes >= 0) {
                                val downloadedBytes: Long =
                                    cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
                                downloadProgress =
                                    (downloadedBytes * 100L / totalSizeInBytes).toInt()

                                emit(State.downloading(downloadProgress))
                            }
                        }
                        DownloadManager.STATUS_SUCCESSFUL -> {
                            downloadProgress = 100
                            emit(State.downloading(downloadProgress))
                            isDownloadFinished = true

                            val downloadedVideo = getDownloadedVideo()
                            emit(State.success(true))
                        }
                    }
                }
            }

MediaStore Code

fun getDownloadedVideo(): MediaStoreVideo {
        lateinit var mediaStoreVideo: MediaStoreVideo

        val projection = arrayOf(
            MediaStore.Video.Media._ID,
            MediaStore.Video.Media.DISPLAY_NAME,
            MediaStore.Video.Media.SIZE,
            MediaStore.Video.Media.DATE_ADDED
        )
        val selection =
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) "${MediaStore.Video.Media.RELATIVE_PATH} like ? " else "${MediaStore.Video.Media.DATA} like ? "
        val selectionArgs =
            arrayOf("%MyApp/CategoryOne%")
        val sortOrder = "${MediaStore.Images.Media.DATE_ADDED} DESC"

        context.contentResolver.query(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            selectionArgs,
            sortOrder
        )?.use { cursor ->
            val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)
            val dateAddedColumn =
                cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_ADDED)
            val displayNameColumn =
                cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)
            val sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)

            if (cursor.moveToFirst()) {
                val id = cursor.getLong(idColumn)
                val contentUri = ContentUris.withAppendedId(
                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    id
                )
                val displayName = cursor.getString(displayNameColumn)
                val size = cursor.getLong(sizeColumn)
                val dateAdded =
                    Date(TimeUnit.SECONDS.toMillis(cursor.getLong(dateAddedColumn)))

                mediaStoreVideo = MediaStoreVideo(id, contentUri, displayName, size, dateAdded)

            }
        }
        return mediaStoreVideo
    }

This is the approach that I have followed to get the info of the video that is last downloaded by my app using DownloadManager. Also, I am not sure if this is the best approach to do the same. Any help will be appreciated. Thanks



from Unable to retrieve latest downloaded Video using MediaStore

No comments:

Post a Comment