Use MediaStore instead of deprecated addCompletedDownload since Android Q.

This commit is contained in:
onurays 2020-03-26 15:58:31 +03:00
parent 4b7da9ae6b
commit 5816a04a37
1 changed files with 18 additions and 1 deletions

View File

@ -17,7 +17,10 @@
package im.vector.riotx.core.files
import android.app.DownloadManager
import android.content.ContentValues
import android.content.Context
import android.os.Build
import android.provider.MediaStore
import androidx.annotation.WorkerThread
import arrow.core.Try
import okio.buffer
@ -57,7 +60,21 @@ fun addEntryToDownloadManager(context: Context,
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?
try {
downloadManager?.addCompletedDownload(title, description, true, mimeType, file.absolutePath, file.length(), true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val contentValues = ContentValues().apply {
put(MediaStore.Downloads.TITLE, title)
put(MediaStore.Downloads.DISPLAY_NAME, description)
put(MediaStore.Downloads.MIME_TYPE, mimeType)
put(MediaStore.Downloads.SIZE, file.length())
}
context.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)?.let { uri ->
context.contentResolver.openOutputStream(uri)?.use { outputStream ->
outputStream.sink().buffer().write(file.inputStream().use { it.readBytes() })
}
}
} else {
downloadManager?.addCompletedDownload(title, description, true, mimeType, file.absolutePath, file.length(), true)
}
} catch (e: Exception) {
Timber.e(e, "## addEntryToDownloadManager(): Exception")
}