Sharing a generated image from an Android WebView app (FileProvider, ClipData)
androidwebviewshare-intentfileproviderclipdatakotlinjavascript-bridge
"Two Cats" is the Duet Cats web game (justinahampton.com/fun/arcade/duetcats/) wrapped as a Kotlin WebView APK. Its "Share score card" button took two releases to work. The fixes apply to any app that shares a file it generated through the system share sheet.
Why the web code does nothing in a WebView
Android WebView has no navigator.share, and its <a download> cannot save blob: URLs. So a share button that works in Chrome silently does nothing in the wrapper. Add a JavaScript bridge (addJavascriptInterface, e.g. TwoCatsAndroid.shareCard(base64Png)) that writes the image to a file, wraps it with a FileProvider content: URI, and launches an ACTION_SEND chooser.
Fix 1: the URI grant must travel on ClipData
With only EXTRA_STREAM plus FLAG_GRANT_READ_URI_PERMISSION, logcat showed Permission Denial: opening provider androidx.core.content.FileProvider for both the chooser and the target, the chooser showed no thumbnail, and the target dropped the attachment. Intent.createChooser() copies a grant to the chooser intent only if the target intent already has ClipData; the EXTRA_STREAM migration happens too late. So:
val send = Intent(Intent.ACTION_SEND).apply {
type = "image/png"
putExtra(Intent.EXTRA_STREAM, uri)
clipData = ClipData.newUri(contentResolver, "score card", uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
val chooser = Intent.createChooser(send, "Share").apply {
clipData = send.clipData
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivity(chooser)
Fix 2: AOSP Messaging refuses files under /data
AOSP Messaging opens the file descriptor, readlinks /proc/self/fd/N, and rejects any content: URI whose backing file resolves under /data ("Cannot send private file", FileUtil.isInPrivateDir). App-internal cache lives under /data. Write the PNG to externalCacheDir (declared as <external-cache-path> in the FileProvider paths XML) and fall back to internal cache only if external storage is unavailable.
How to verify
"The APK downloaded" is not verification (v1.2.0 shipped on that basis). On a real device: trigger the share, confirm the share sheet shows the image thumbnail (a missing thumbnail means the grant did not reach the chooser), pick Messaging, and confirm the draft contains the image. adb logcat | grep -E 'Permission Denial|Cannot send' must stay empty. The test device was a Pixel 2 XL on Android 15 with AOSP Messaging.
Wrapping a web game as an APK
- Keep the web game as the single source of truth; a
sync-assets.shcopies it into the APK assets and patches it (rename, absolute URLs for server endpoints, no service worker, inject anapk.jsshim). Make each patch assert on its anchor string and fail loudly when the web code changes. - Read
versionCode/versionNamefrom a changelog JSON in Gradle so a release cannot skip the changelog; the app's own menu checks a server JSON for updates (see In-app updates for sideloaded Android apps). adb install -rwith the same signing key keeps app data; keep the keystore andkey.propertiesuntracked and backed up (Android app signing keys and why losing one is permanent).