Making a Flutter app an Android share target (native, no plugin)
androidfluttershare-intenteventchannelkotlinintent-filteruploadsplugins
TLDR appears in Android's share sheet: share a photo, video, PDF or any file and it opens a new post
with the file uploaded and embedded. It is implemented with about 80 lines of Kotlin and 30 lines of
Dart over an EventChannel, because every share plugin on pub.dev failed to build with a modern
toolchain. The pattern generalizes to any small Android integration.
Why not a plugin
receive_sharing_intent (stale, hardcoded Kotlin 1.9.22 / JVM 1.8), share_handler (open build
errors, Kotlin 1.8.22) and flutter_sharing_intent (JVM 1.8) all produced JVM target mismatch
errors against Kotlin 2.2, AGP 8.11, Gradle 8.14 and JVM 17. Rule that came out of it: before
flutter pub add, check last release date (reject if stale beyond ~6 months), its Kotlin/AGP/JVM
targets versus yours, and open build-failure issues; for small platform features prefer a native
MethodChannel/EventChannel.
Manifest
Declare the widest filter you can actually handle. The first version listed image/*,
application/pdf, text/plain, application/octet-stream, so videos, audio, zips and Office
documents never offered the app. The working filter:
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
</intent-filter>
Kotlin side (MainActivity)
- Handle
ACTION_SENDandACTION_SEND_MULTIPLEin bothonCreateandonNewIntent. - Copy each
EXTRA_STREAMURI into the app cache asshare_<timestamp>_<displayname>, queryingOpenableColumns.DISPLAY_NAMEfor the real name. - If the display name has no extension, derive one:
contentResolver.getType(uri)thenMimeTypeMap.getSingleton().getExtensionFromMimeType(...). Replace/in names. - Do the copy on a background thread, then hand paths over with
runOnUiThread; copying a large video on the main thread causes an ANR. - Keep a
pendingSharedFilesbuffer for intents that arrive before the Dart listener attaches (cold start), and flush it whenonListenfires. - A text-only share (no
EXTRA_STREAM) needs separate handling if you want it.
Dart side
- A service listens on
EventChannel("<package>/share"), stores pending paths, and fires a callback that navigates to the composer (/new_post?shared=true). - The composer strips the
share_<timestamp>_prefix, uploads each file and inserts markdown. - Stream big files from disk (
MultipartFile.fromPath), neverreadAsBytesa video into memory. Read images into memory only when you need them (e.g. to probe dimensions). - Scale the upload timeout with size (here 5 minutes plus 1 minute per 10 MB) and map 413 and timeouts to clear messages.
- Pre-check size against the server's real limit, and surface failed uploads; a silently ignored failure produced an empty post.
- Disable Submit while shared uploads are in flight; delete the cache copy afterwards.
The 2 MB phantom limit
After this shipped, a 7 MB photo was rejected as "too large (max 2 MB)" although the server allowed
500 MB. The client's upload-status call omitted the auth token, got 401, swallowed it, and kept a
hardcoded 2 MB default. Fix: send the token, and make the fallback default generous so a failed
status check can never block a file the server would accept. Found by grepping the nginx access
log for 401s from the app's Dart/ user agent. See Debugging client-side bugs from the server's access log.
Gotchas
- Git worktrees of a Flutter Android app need the untracked
android/app/google-services.jsoncopied in or the Gradle build fails. - Test with real file types you care about (video, a file with no extension), not just photos.
See also
- Android app signing keys and why losing one is permanent
- Accepting and serving user uploads safely from a private bucket