diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/AndroidLocalMediaActions.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/AndroidLocalMediaActions.kt index eb1773042b..65cc2e45bc 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/AndroidLocalMediaActions.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/AndroidLocalMediaActions.kt @@ -16,6 +16,7 @@ package io.element.android.features.messages.impl.media.local +import android.content.ContentResolver import android.content.ContentValues import android.content.Context import android.content.Intent @@ -45,6 +46,7 @@ class AndroidLocalMediaActions @Inject constructor( ) : LocalMediaActions { override suspend fun saveOnDisk(localMedia: LocalMedia): Result = withContext(coroutineDispatchers.io) { + require(localMedia.uri.scheme == ContentResolver.SCHEME_FILE) runCatching { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { saveOnDiskUsingMediaStore(localMedia) @@ -59,6 +61,7 @@ class AndroidLocalMediaActions @Inject constructor( } override suspend fun share(localMedia: LocalMedia): Result = withContext(coroutineDispatchers.io) { + require(localMedia.uri.scheme == ContentResolver.SCHEME_FILE) runCatching { val authority = "${buildMeta.applicationId}.fileprovider" val uriFromFileProvider = FileProvider.getUriForFile(context, authority, localMedia.toFile()) @@ -109,7 +112,17 @@ class AndroidLocalMediaActions @Inject constructor( return context.contentResolver.openInputStream(uri) } + /** + * Tries to extract a file from the uri and rename it using the local media name if defined. + */ private fun LocalMedia.toFile(): File { - return uri.toFile() + val uriAsFile = uri.toFile() + return if (name != null) { + File(uriAsFile.parentFile, name).apply { + uriAsFile.renameTo(this) + } + } else { + uriAsFile + } } } diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaActions.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaActions.kt index f7c37bd14a..03f09488df 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaActions.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/local/LocalMediaActions.kt @@ -17,7 +17,16 @@ package io.element.android.features.messages.impl.media.local interface LocalMediaActions { + /** + * Will save the current media to the Downloads directory. + * The [LocalMedia.uri] needs to have a file scheme. + */ suspend fun saveOnDisk(localMedia: LocalMedia): Result + + /** + * Will try to find a suitable application to share the media with. + * The [LocalMedia.uri] needs to have a file scheme. + */ suspend fun share(localMedia: LocalMedia): Result }