Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ShareCompat.IntentBuilder for sharing images #3965

Merged
merged 1 commit into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3965.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enable image preview in Android's share sheet (Android 11+)
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.activity.result.ActivityResultLauncher
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import androidx.browser.customtabs.CustomTabsSession
import androidx.core.app.ShareCompat
import androidx.core.content.FileProvider
import androidx.core.content.getSystemService
import im.vector.app.BuildConfig
Expand Down Expand Up @@ -297,23 +298,19 @@ fun openMedia(activity: Activity, savedMediaPath: String, mimeType: String) {
}

fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
var mediaUri: Uri? = null
try {
mediaUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file)
val mediaUri = try {
FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file)
} catch (e: Exception) {
Timber.e(e, "onMediaAction Selected File cannot be shared")
return
}

if (null != mediaUri) {
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
// Grant temporary read permission to the content URI
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
sendIntent.type = mediaMimeType
sendIntent.putExtra(Intent.EXTRA_STREAM, mediaUri)
val sendIntent = ShareCompat.IntentBuilder(context)
.setType(mediaMimeType)
.setStream(mediaUri)
.getIntent()

sendShareIntent(context, sendIntent)
}
sendShareIntent(context, sendIntent)
}

fun shareText(context: Context, text: String) {
Expand Down