Use MediaStore instead of deprecated addCompletedDownload since Android Q.
This commit is contained in:
parent
4b7da9ae6b
commit
5816a04a37
@ -17,7 +17,10 @@
|
|||||||
package im.vector.riotx.core.files
|
package im.vector.riotx.core.files
|
||||||
|
|
||||||
import android.app.DownloadManager
|
import android.app.DownloadManager
|
||||||
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
|
import android.provider.MediaStore
|
||||||
import androidx.annotation.WorkerThread
|
import androidx.annotation.WorkerThread
|
||||||
import arrow.core.Try
|
import arrow.core.Try
|
||||||
import okio.buffer
|
import okio.buffer
|
||||||
@ -57,7 +60,21 @@ fun addEntryToDownloadManager(context: Context,
|
|||||||
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?
|
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?
|
||||||
|
|
||||||
try {
|
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) {
|
} catch (e: Exception) {
|
||||||
Timber.e(e, "## addEntryToDownloadManager(): Exception")
|
Timber.e(e, "## addEntryToDownloadManager(): Exception")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user