Give user a toast after adding media file to the gallery.

This commit is contained in:
onurays 2020-04-17 16:53:40 +03:00
parent 8434f9326e
commit 26bb8ce2be
3 changed files with 12 additions and 3 deletions

View file

@ -260,7 +260,7 @@ fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
} }
} }
fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String?) { fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String?): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val externalContentUri: Uri val externalContentUri: Uri
val values = ContentValues() val values = ContentValues()
@ -301,6 +301,7 @@ fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String
context.contentResolver.insert(externalContentUri, values)?.let { uri -> context.contentResolver.insert(externalContentUri, values)?.let { uri ->
context.contentResolver.openOutputStream(uri)?.use { outputStream -> context.contentResolver.openOutputStream(uri)?.use { outputStream ->
outputStream.sink().buffer().write(file.inputStream().use { it.readBytes() }) outputStream.sink().buffer().write(file.inputStream().use { it.readBytes() })
return true
} }
} }
} else { } else {
@ -309,7 +310,9 @@ fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String
mediaScanIntent.data = Uri.fromFile(file) mediaScanIntent.data = Uri.fromFile(file)
context.sendBroadcast(mediaScanIntent) context.sendBroadcast(mediaScanIntent)
} }
return true
} }
return false
} }
/** /**

View file

@ -1164,12 +1164,17 @@ class RoomDetailFragment @Inject constructor(
object : MatrixCallback<File> { object : MatrixCallback<File> {
override fun onSuccess(data: File) { override fun onSuccess(data: File) {
if (isAdded) { if (isAdded) {
saveMedia( val saved = saveMedia(
context = requireContext(), context = requireContext(),
file = data, file = data,
title = action.messageContent.body, title = action.messageContent.body,
mediaMimeType = getMimeTypeFromUri(requireContext(), data.toUri()) mediaMimeType = getMimeTypeFromUri(requireContext(), data.toUri())
) )
if (saved) {
Toast.makeText(requireContext(), R.string.media_file_added_to_gallery, Toast.LENGTH_LONG).show()
} else {
Toast.makeText(requireContext(), R.string.error_adding_media_file_to_gallery, Toast.LENGTH_LONG).show()
}
} }
} }
} }

View file

@ -21,7 +21,8 @@
<!-- BEGIN Strings added by Onuray --> <!-- BEGIN Strings added by Onuray -->
<string name="media_file_added_to_gallery">Media file added to the Gallery</string>
<string name="error_adding_media_file_to_gallery">Could not add media file to the Gallery</string>
<!-- END Strings added by Onuray --> <!-- END Strings added by Onuray -->