From 91ea60d43a2bb38c968c17ef758f7aa9f34dd5b7 Mon Sep 17 00:00:00 2001 From: kppro Date: Mon, 8 Mar 2021 18:13:27 +0000 Subject: [PATCH] on android, added the capibility to handle file received from intent action_view --- .../ReceiveSharingIntentPlugin.kt | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/android/src/main/kotlin/com/kasem/receive_sharing_intent/ReceiveSharingIntentPlugin.kt b/android/src/main/kotlin/com/kasem/receive_sharing_intent/ReceiveSharingIntentPlugin.kt index 0d7bfa3a..90c0c7f7 100644 --- a/android/src/main/kotlin/com/kasem/receive_sharing_intent/ReceiveSharingIntentPlugin.kt +++ b/android/src/main/kotlin/com/kasem/receive_sharing_intent/ReceiveSharingIntentPlugin.kt @@ -113,7 +113,8 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl private fun handleIntent(intent: Intent, initial: Boolean) { when { (intent.type?.startsWith("text") != true) - && (intent.action == Intent.ACTION_SEND + && (intent.action == Intent.ACTION_VIEW + || intent.action == Intent.ACTION_SEND || intent.action == Intent.ACTION_SEND_MULTIPLE) -> { // Sharing images or videos val value = getMediaUris(intent) @@ -137,25 +138,32 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl } } + private fun getMediaFromUri(uri: Uri): JSONArray? { + val path = FileDirectory.getAbsolutePath(applicationContext, uri) + + return if (path != null) { + val type = getMediaType(path) + val thumbnail = getThumbnail(path, type) + val duration = getDuration(path, type) + JSONArray().put( + JSONObject() + .put("path", path) + .put("type", type.ordinal) + .put("thumbnail", thumbnail) + .put("duration", duration) + ) + } else null + } + private fun getMediaUris(intent: Intent?): JSONArray? { if (intent == null) return null return when (intent.action) { + Intent.ACTION_VIEW -> { + return getMediaFromUri(intent.getData()) + } Intent.ACTION_SEND -> { - val uri = intent.getParcelableExtra(Intent.EXTRA_STREAM) - val path = FileDirectory.getAbsolutePath(applicationContext, uri) - if (path != null) { - val type = getMediaType(path) - val thumbnail = getThumbnail(path, type) - val duration = getDuration(path, type) - JSONArray().put( - JSONObject() - .put("path", path) - .put("type", type.ordinal) - .put("thumbnail", thumbnail) - .put("duration", duration) - ) - } else null + return getMediaFromUri(intent.getParcelableExtra(Intent.EXTRA_STREAM)) } Intent.ACTION_SEND_MULTIPLE -> { val uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM)