Improved media store insertion
This commit is contained in:
parent
bc511c1bc1
commit
c5f40c8eed
2 changed files with 80 additions and 95 deletions
|
@ -24,6 +24,7 @@ import android.app.Activity
|
||||||
import android.bluetooth.BluetoothAdapter
|
import android.bluetooth.BluetoothAdapter
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.os.Vibrator
|
import android.os.Vibrator
|
||||||
|
@ -88,22 +89,12 @@ class Api21Compatibility {
|
||||||
put(MediaStore.Images.Media.MIME_TYPE, mime)
|
put(MediaStore.Images.Media.MIME_TYPE, mime)
|
||||||
}
|
}
|
||||||
val collection = MediaStore.Images.Media.getContentUri("external")
|
val collection = MediaStore.Images.Media.getContentUri("external")
|
||||||
|
val mediaStoreFilePath = addContentValuesToCollection(context, filePath, collection, values)
|
||||||
val fileUri = context.contentResolver.insert(collection, values)
|
if (mediaStoreFilePath.isNotEmpty()) {
|
||||||
if (fileUri == null) {
|
content.userData = mediaStoreFilePath
|
||||||
Log.e("[Media Store] Failed to get a URI to where store the file, aborting")
|
return true
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
var copyOk = false
|
|
||||||
context.contentResolver.openOutputStream(fileUri).use { out ->
|
|
||||||
copyOk = FileUtils.copyFileTo(filePath, out)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (copyOk) {
|
|
||||||
content.userData = fileUri.toString()
|
|
||||||
}
|
|
||||||
return copyOk
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun addVideoToMediaStore(context: Context, content: Content): Boolean {
|
suspend fun addVideoToMediaStore(context: Context, content: Content): Boolean {
|
||||||
|
@ -130,22 +121,12 @@ class Api21Compatibility {
|
||||||
put(MediaStore.Video.Media.MIME_TYPE, mime)
|
put(MediaStore.Video.Media.MIME_TYPE, mime)
|
||||||
}
|
}
|
||||||
val collection = MediaStore.Video.Media.getContentUri("external")
|
val collection = MediaStore.Video.Media.getContentUri("external")
|
||||||
|
val mediaStoreFilePath = addContentValuesToCollection(context, filePath, collection, values)
|
||||||
val fileUri = context.contentResolver.insert(collection, values)
|
if (mediaStoreFilePath.isNotEmpty()) {
|
||||||
if (fileUri == null) {
|
content.userData = mediaStoreFilePath
|
||||||
Log.e("[Media Store] Failed to get a URI to where store the file, aborting")
|
return true
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
var copyOk = false
|
|
||||||
context.contentResolver.openOutputStream(fileUri).use { out ->
|
|
||||||
copyOk = FileUtils.copyFileTo(filePath, out)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (copyOk) {
|
|
||||||
content.userData = fileUri.toString()
|
|
||||||
}
|
|
||||||
return copyOk
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun addAudioToMediaStore(context: Context, content: Content): Boolean {
|
suspend fun addAudioToMediaStore(context: Context, content: Content): Boolean {
|
||||||
|
@ -173,21 +154,36 @@ class Api21Compatibility {
|
||||||
}
|
}
|
||||||
val collection = MediaStore.Audio.Media.getContentUri("external")
|
val collection = MediaStore.Audio.Media.getContentUri("external")
|
||||||
|
|
||||||
val fileUri = context.contentResolver.insert(collection, values)
|
val mediaStoreFilePath = addContentValuesToCollection(context, filePath, collection, values)
|
||||||
if (fileUri == null) {
|
if (mediaStoreFilePath.isNotEmpty()) {
|
||||||
Log.e("[Media Store] Failed to get a URI to where store the file, aborting")
|
content.userData = mediaStoreFilePath
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
var copyOk = false
|
private suspend fun addContentValuesToCollection(
|
||||||
context.contentResolver.openOutputStream(fileUri).use { out ->
|
context: Context,
|
||||||
copyOk = FileUtils.copyFileTo(filePath, out)
|
filePath: String,
|
||||||
}
|
collection: Uri,
|
||||||
|
values: ContentValues
|
||||||
|
): String {
|
||||||
|
try {
|
||||||
|
val fileUri = context.contentResolver.insert(collection, values)
|
||||||
|
if (fileUri == null) {
|
||||||
|
Log.e("[Media Store] Failed to get a URI to where store the file, aborting")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
if (copyOk) {
|
context.contentResolver.openOutputStream(fileUri).use { out ->
|
||||||
content.userData = fileUri.toString()
|
if (FileUtils.copyFileTo(filePath, out)) {
|
||||||
|
return fileUri.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e("[Media Store] Exception: $e")
|
||||||
}
|
}
|
||||||
return copyOk
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setShowWhenLocked(activity: Activity, enable: Boolean) {
|
fun setShowWhenLocked(activity: Activity, enable: Boolean) {
|
||||||
|
|
|
@ -24,6 +24,7 @@ import android.app.NotificationManager
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
@ -82,26 +83,12 @@ class Api29Compatibility {
|
||||||
put(MediaStore.Images.Media.IS_PENDING, 1)
|
put(MediaStore.Images.Media.IS_PENDING, 1)
|
||||||
}
|
}
|
||||||
val collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
val collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
||||||
|
val mediaStoreFilePath = addContentValuesToCollection(context, filePath, collection, values, MediaStore.Images.Media.IS_PENDING)
|
||||||
val fileUri = context.contentResolver.insert(collection, values)
|
if (mediaStoreFilePath.isNotEmpty()) {
|
||||||
if (fileUri == null) {
|
content.userData = mediaStoreFilePath
|
||||||
Log.e("[Media Store] Failed to get a URI to where store the file, aborting")
|
return true
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
var copyOk = false
|
|
||||||
context.contentResolver.openOutputStream(fileUri).use { out ->
|
|
||||||
copyOk = FileUtils.copyFileTo(filePath, out)
|
|
||||||
}
|
|
||||||
|
|
||||||
values.clear()
|
|
||||||
values.put(MediaStore.Images.Media.IS_PENDING, 0)
|
|
||||||
context.contentResolver.update(fileUri, values, null, null)
|
|
||||||
|
|
||||||
if (copyOk) {
|
|
||||||
content.userData = fileUri.toString()
|
|
||||||
}
|
|
||||||
return copyOk
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun addVideoToMediaStore(context: Context, content: Content): Boolean {
|
suspend fun addVideoToMediaStore(context: Context, content: Content): Boolean {
|
||||||
|
@ -125,26 +112,12 @@ class Api29Compatibility {
|
||||||
put(MediaStore.Video.Media.IS_PENDING, 1)
|
put(MediaStore.Video.Media.IS_PENDING, 1)
|
||||||
}
|
}
|
||||||
val collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
val collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
||||||
|
val mediaStoreFilePath = addContentValuesToCollection(context, filePath, collection, values, MediaStore.Video.Media.IS_PENDING)
|
||||||
val fileUri = context.contentResolver.insert(collection, values)
|
if (mediaStoreFilePath.isNotEmpty()) {
|
||||||
if (fileUri == null) {
|
content.userData = mediaStoreFilePath
|
||||||
Log.e("[Media Store] Failed to get a URI to where store the file, aborting")
|
return true
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
var copyOk = false
|
|
||||||
context.contentResolver.openOutputStream(fileUri).use { out ->
|
|
||||||
copyOk = FileUtils.copyFileTo(filePath, out)
|
|
||||||
}
|
|
||||||
|
|
||||||
values.clear()
|
|
||||||
values.put(MediaStore.Video.Media.IS_PENDING, 0)
|
|
||||||
context.contentResolver.update(fileUri, values, null, null)
|
|
||||||
|
|
||||||
if (copyOk) {
|
|
||||||
content.userData = fileUri.toString()
|
|
||||||
}
|
|
||||||
return copyOk
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun addAudioToMediaStore(context: Context, content: Content): Boolean {
|
suspend fun addAudioToMediaStore(context: Context, content: Content): Boolean {
|
||||||
|
@ -169,25 +142,41 @@ class Api29Compatibility {
|
||||||
}
|
}
|
||||||
val collection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
val collection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
||||||
|
|
||||||
val fileUri = context.contentResolver.insert(collection, values)
|
val mediaStoreFilePath = addContentValuesToCollection(context, filePath, collection, values, MediaStore.Audio.Media.IS_PENDING)
|
||||||
if (fileUri == null) {
|
if (mediaStoreFilePath.isNotEmpty()) {
|
||||||
Log.e("[Media Store] Failed to get a URI to where store the file, aborting")
|
content.userData = mediaStoreFilePath
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
var copyOk = false
|
private suspend fun addContentValuesToCollection(
|
||||||
context.contentResolver.openOutputStream(fileUri).use { out ->
|
context: Context,
|
||||||
copyOk = FileUtils.copyFileTo(filePath, out)
|
filePath: String,
|
||||||
|
collection: Uri,
|
||||||
|
values: ContentValues,
|
||||||
|
pendingKey: String
|
||||||
|
): String {
|
||||||
|
try {
|
||||||
|
val fileUri = context.contentResolver.insert(collection, values)
|
||||||
|
if (fileUri == null) {
|
||||||
|
Log.e("[Media Store] Failed to get a URI to where store the file, aborting")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
context.contentResolver.openOutputStream(fileUri).use { out ->
|
||||||
|
if (FileUtils.copyFileTo(filePath, out)) {
|
||||||
|
values.clear()
|
||||||
|
values.put(pendingKey, 0)
|
||||||
|
context.contentResolver.update(fileUri, values, null, null)
|
||||||
|
|
||||||
|
return fileUri.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e("[Media Store] Exception: $e")
|
||||||
}
|
}
|
||||||
|
return ""
|
||||||
values.clear()
|
|
||||||
values.put(MediaStore.Audio.Media.IS_PENDING, 0)
|
|
||||||
context.contentResolver.update(fileUri, values, null, null)
|
|
||||||
|
|
||||||
if (copyOk) {
|
|
||||||
content.userData = fileUri.toString()
|
|
||||||
}
|
|
||||||
return copyOk
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue