FIX Telegram Mime image share

This commit is contained in:
Dwindi Ramadhana
2026-03-22 19:37:49 +07:00
parent e34dd6cf06
commit c32b56c00e
19 changed files with 942 additions and 24 deletions

View File

@@ -1,21 +1,29 @@
package com.jamshalat.app
import android.content.Intent
import android.content.pm.PackageManager
import android.hardware.GeomagneticField
import androidx.core.content.FileProvider
import com.ryanheise.audioservice.AudioServiceActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import java.io.File
class MainActivity : AudioServiceActivity() {
companion object {
private const val GEOMAGNETIC_CHANNEL = "com.jamshalat.app/geomagnetic"
private const val SHARE_CHANNEL = "com.jamshalat.app/share"
private const val DECLINATION_METHOD = "getDeclination"
private const val SHARE_IMAGE_METHOD = "shareImage"
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, GEOMAGNETIC_CHANNEL)
.setMethodCallHandler(::handleGeomagneticMethodCall)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, SHARE_CHANNEL)
.setMethodCallHandler(::handleShareMethodCall)
}
private fun handleGeomagneticMethodCall(call: MethodCall, result: MethodChannel.Result) {
@@ -41,4 +49,53 @@ class MainActivity : AudioServiceActivity() {
)
result.success(field.declination.toDouble())
}
private fun handleShareMethodCall(call: MethodCall, result: MethodChannel.Result) {
if (call.method != SHARE_IMAGE_METHOD) {
result.notImplemented()
return
}
val path = call.argument<String>("path")
if (path.isNullOrBlank()) {
result.error("INVALID_ARGS", "Path is required.", null)
return
}
val file = File(path)
if (!file.exists()) {
result.error("FILE_NOT_FOUND", "Shared image file does not exist.", null)
return
}
try {
val mimeType = call.argument<String>("mimeType") ?: "image/png"
val chooserTitle = call.argument<String>("chooserTitle")
val authority = "${applicationContext.packageName}.flutter.share_provider"
val fileUri = FileProvider.getUriForFile(this, authority, file)
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = mimeType
putExtra(Intent.EXTRA_STREAM, fileUri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
val chooserIntent = Intent.createChooser(shareIntent, chooserTitle)
packageManager.queryIntentActivities(
chooserIntent,
PackageManager.MATCH_DEFAULT_ONLY,
).forEach { resolveInfo ->
grantUriPermission(
resolveInfo.activityInfo.packageName,
fileUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION,
)
}
startActivity(chooserIntent)
result.success(null)
} catch (error: Throwable) {
result.error("SHARE_FAILED", error.message, null)
}
}
}

Binary file not shown.

Binary file not shown.