Media Viewer: rename the shared file with the known name if any.
This commit is contained in:
parent
235efeadb6
commit
c2fa9e6add
2 changed files with 23 additions and 1 deletions
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package io.element.android.features.messages.impl.media.local
|
package io.element.android.features.messages.impl.media.local
|
||||||
|
|
||||||
|
import android.content.ContentResolver
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
|
@ -45,6 +46,7 @@ class AndroidLocalMediaActions @Inject constructor(
|
||||||
) : LocalMediaActions {
|
) : LocalMediaActions {
|
||||||
|
|
||||||
override suspend fun saveOnDisk(localMedia: LocalMedia): Result<Unit> = withContext(coroutineDispatchers.io) {
|
override suspend fun saveOnDisk(localMedia: LocalMedia): Result<Unit> = withContext(coroutineDispatchers.io) {
|
||||||
|
require(localMedia.uri.scheme == ContentResolver.SCHEME_FILE)
|
||||||
runCatching {
|
runCatching {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
saveOnDiskUsingMediaStore(localMedia)
|
saveOnDiskUsingMediaStore(localMedia)
|
||||||
|
|
@ -59,6 +61,7 @@ class AndroidLocalMediaActions @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun share(localMedia: LocalMedia): Result<Unit> = withContext(coroutineDispatchers.io) {
|
override suspend fun share(localMedia: LocalMedia): Result<Unit> = withContext(coroutineDispatchers.io) {
|
||||||
|
require(localMedia.uri.scheme == ContentResolver.SCHEME_FILE)
|
||||||
runCatching {
|
runCatching {
|
||||||
val authority = "${buildMeta.applicationId}.fileprovider"
|
val authority = "${buildMeta.applicationId}.fileprovider"
|
||||||
val uriFromFileProvider = FileProvider.getUriForFile(context, authority, localMedia.toFile())
|
val uriFromFileProvider = FileProvider.getUriForFile(context, authority, localMedia.toFile())
|
||||||
|
|
@ -109,7 +112,17 @@ class AndroidLocalMediaActions @Inject constructor(
|
||||||
return context.contentResolver.openInputStream(uri)
|
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 {
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,16 @@
|
||||||
package io.element.android.features.messages.impl.media.local
|
package io.element.android.features.messages.impl.media.local
|
||||||
|
|
||||||
interface LocalMediaActions {
|
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<Unit>
|
suspend fun saveOnDisk(localMedia: LocalMedia): Result<Unit>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<Unit>
|
suspend fun share(localMedia: LocalMedia): Result<Unit>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue