From c61707a35847a77bca9a09af7b43c41e7e17ddf6 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Mon, 9 May 2022 14:09:10 +0400 Subject: [PATCH] android: save images to Gallery (#622) --- apps/android/app/src/main/AndroidManifest.xml | 1 + .../app/views/chat/item/ChatItemView.kt | 6 ++- .../chat/simplex/app/views/helpers/Share.kt | 46 +++++++++++++------ .../app/src/main/res/values-ru/strings.xml | 1 + .../app/src/main/res/values/strings.xml | 1 + 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/apps/android/app/src/main/AndroidManifest.xml b/apps/android/app/src/main/AndroidManifest.xml index 113ffa93e2..5062b90ed6 100644 --- a/apps/android/app/src/main/AndroidManifest.xml +++ b/apps/android/app/src/main/AndroidManifest.xml @@ -15,6 +15,7 @@ + saveImage(context, cItem.file) + is MsgContent.MCFile -> saveFileLauncher.launch(cItem.file?.fileName) + else -> {} + } showMenu.value = false }) } diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/helpers/Share.kt b/apps/android/app/src/main/java/chat/simplex/app/views/helpers/Share.kt index 6c66b2bbb8..b02f0c535f 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/helpers/Share.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/helpers/Share.kt @@ -2,6 +2,7 @@ package chat.simplex.app.views.helpers import android.content.* import android.net.Uri +import android.provider.MediaStore import android.widget.Toast import androidx.activity.compose.ManagedActivityResultLauncher import androidx.activity.compose.rememberLauncherForActivityResult @@ -10,8 +11,7 @@ import androidx.compose.runtime.Composable import androidx.core.content.ContextCompat import chat.simplex.app.R import chat.simplex.app.model.CIFile -import java.io.File -import java.io.IOException +import java.io.* fun shareText(cxt: Context, text: String) { val sendIntent: Intent = Intent().apply { @@ -33,20 +33,16 @@ fun rememberSaveFileLauncher(cxt: Context, ciFile: CIFile?): ManagedActivityResu rememberLauncherForActivityResult( contract = ActivityResultContracts.CreateDocument(), onResult = { destination -> - if (destination != null) { + destination?.let { val filePath = getLoadedFilePath(cxt, ciFile) if (filePath != null) { val contentResolver = cxt.contentResolver - val file = File(filePath) - try { - val outputStream = contentResolver.openOutputStream(destination) - if (outputStream != null) { - outputStream.write(file.readBytes()) - outputStream.close() - Toast.makeText(cxt, generalGetString(R.string.file_saved), Toast.LENGTH_SHORT).show() - } - } catch (e: IOException) { - Toast.makeText(cxt, generalGetString(R.string.error_saving_file), Toast.LENGTH_SHORT).show() + contentResolver.openOutputStream(destination)?.let { stream -> + val outputStream = BufferedOutputStream(stream) + val file = File(filePath) + outputStream.write(file.readBytes()) + outputStream.close() + Toast.makeText(cxt, generalGetString(R.string.file_saved), Toast.LENGTH_SHORT).show() } } else { Toast.makeText(cxt, generalGetString(R.string.file_not_found), Toast.LENGTH_SHORT).show() @@ -54,3 +50,27 @@ fun rememberSaveFileLauncher(cxt: Context, ciFile: CIFile?): ManagedActivityResu } } ) + +fun saveImage(cxt: Context, ciFile: CIFile?) { + val filePath = getLoadedFilePath(cxt, ciFile) + val fileName = ciFile?.fileName + if (filePath != null && fileName != null) { + val values = ContentValues() + values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()) + values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg") + values.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName) + values.put(MediaStore.MediaColumns.TITLE, fileName) + val uri = cxt.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values) + uri?.let { + cxt.contentResolver.openOutputStream(uri)?.let { stream -> + val outputStream = BufferedOutputStream(stream) + val file = File(filePath) + outputStream.write(file.readBytes()) + outputStream.close() + Toast.makeText(cxt, generalGetString(R.string.image_saved), Toast.LENGTH_SHORT).show() + } + } + } else { + Toast.makeText(cxt, generalGetString(R.string.file_not_found), Toast.LENGTH_SHORT).show() + } +} diff --git a/apps/android/app/src/main/res/values-ru/strings.xml b/apps/android/app/src/main/res/values-ru/strings.xml index 8e2d39232a..7ab856193a 100644 --- a/apps/android/app/src/main/res/values-ru/strings.xml +++ b/apps/android/app/src/main/res/values-ru/strings.xml @@ -99,6 +99,7 @@ Изображение отправлено Ожидается прием изображения Изображение будет принято, когда ваш контакт будет в сети, подождите или проверьте позже! + Изображение сохранено в Галерею Файл diff --git a/apps/android/app/src/main/res/values/strings.xml b/apps/android/app/src/main/res/values/strings.xml index e6bcd98aa3..6e66cb3bb8 100644 --- a/apps/android/app/src/main/res/values/strings.xml +++ b/apps/android/app/src/main/res/values/strings.xml @@ -99,6 +99,7 @@ Image sent Waiting for image Image will be received when your contact is online, please wait or check later! + Image saved to Gallery File