mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-16 02:05:06 +08:00
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
|
||||
|
||||
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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user