mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-01-15 11:16:17 +01:00
Use ShareCompat.IntentBuilder for sharing images
ShareCompat does the right things so image previews show up in the Share sheet of Android 11.
This commit is contained in:
parent
1fdc5fd3d9
commit
0aff0c1f20
1
changelog.d/3965.bugfix
Normal file
1
changelog.d/3965.bugfix
Normal file
@ -0,0 +1 @@
|
|||||||
|
Enable image preview in Android's share sheet (Android 11+)
|
@ -36,6 +36,7 @@ import androidx.activity.result.ActivityResultLauncher
|
|||||||
import androidx.browser.customtabs.CustomTabColorSchemeParams
|
import androidx.browser.customtabs.CustomTabColorSchemeParams
|
||||||
import androidx.browser.customtabs.CustomTabsIntent
|
import androidx.browser.customtabs.CustomTabsIntent
|
||||||
import androidx.browser.customtabs.CustomTabsSession
|
import androidx.browser.customtabs.CustomTabsSession
|
||||||
|
import androidx.core.app.ShareCompat
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
import androidx.core.content.getSystemService
|
import androidx.core.content.getSystemService
|
||||||
import im.vector.app.BuildConfig
|
import im.vector.app.BuildConfig
|
||||||
@ -297,23 +298,19 @@ fun openMedia(activity: Activity, savedMediaPath: String, mimeType: String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
|
fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
|
||||||
var mediaUri: Uri? = null
|
val mediaUri = try {
|
||||||
try {
|
FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file)
|
||||||
mediaUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.e(e, "onMediaAction Selected File cannot be shared")
|
Timber.e(e, "onMediaAction Selected File cannot be shared")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null != mediaUri) {
|
val sendIntent = ShareCompat.IntentBuilder(context)
|
||||||
val sendIntent = Intent()
|
.setType(mediaMimeType)
|
||||||
sendIntent.action = Intent.ACTION_SEND
|
.setStream(mediaUri)
|
||||||
// Grant temporary read permission to the content URI
|
.getIntent()
|
||||||
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
||||||
sendIntent.type = mediaMimeType
|
|
||||||
sendIntent.putExtra(Intent.EXTRA_STREAM, mediaUri)
|
|
||||||
|
|
||||||
sendShareIntent(context, sendIntent)
|
sendShareIntent(context, sendIntent)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun shareText(context: Context, text: String) {
|
fun shareText(context: Context, text: String) {
|
||||||
|
Loading…
Reference in New Issue
Block a user