← Nesim Apps

Local HTML With Images on Android and iOS: Use Data URIs

By Nesim Razon, independent mobile developer · 2026-11-30

#Android#iOS#Jetpack Compose

An old app of mine shows a few bundled HTML pages with pictures, including one page with seventy logos. When I moved it to Compose Multiplatform, both platforms had to render the same pages from the same files. Turning the images into data URIs made that simple.

The problem with relative image paths

The pages are ordinary HTML fragments with image tags that name a file next to them. In the old Android app those files sat in the assets folder, and a web view could be pointed at it.

In a Compose Multiplatform module the files live in the shared resources. They are read through a function that returns bytes. I did not want to depend on where each platform keeps them on disk, or to write two different ways of turning a resource name into a file address.

Inline the images before showing the page

The solution is to give the web view a single self contained string. Before rendering, a short function walks through the HTML, finds every source attribute that is a plain relative name and reads that file from the resources. It then replaces the name with a data URI: the media type, followed by the file contents encoded in base64.

The pattern skips anything that already contains a colon, so web addresses and existing data URIs are left alone. If a file cannot be read, that attribute stays as it was and the rest of the page still renders. The base64 encoder in the Kotlin standard library is enough, so no extra dependency is needed.

What it costs

Base64 makes data about a third larger. That matters for big photographs and hardly at all for small logos. My whole set is eighty two images and roughly seven hundred kilobytes, and only the images of the open page are encoded.

The work runs in a coroutine when the page opens, and the result is remembered for that page, so scrolling and recomposition do not repeat it. For pages with many large photos I would not use this approach. I would copy the files to a cache folder or serve them through a custom scheme handler.

One function builds the page

The same function wraps the fragment in a complete document on both platforms. It adds the viewport tag and a small style sheet with the colours of the current theme, so the page follows light and dark mode.

Two details of the old content are handled with style rules and not by editing the files. The heading that repeats the screen title is hidden. On the references page the logos are laid out three to a row on white cards, so that dark logos stay visible on a dark background.

The Android side

The Android implementation is a WebView inside an AndroidView. It loads the string with a base address of the website, so any remaining relative link resolves to the real site.

JavaScript is switched off, because these pages do not need it. A tap on a link is intercepted and opened outside the app, which keeps the web view from turning into a small browser with no address bar.

The iOS side

On iOS the same string goes into a WKWebView with JavaScript disabled in the page preferences, and again with the website as the base address. A navigation delegate cancels link activations and hands the address to the app, which opens it outside.

Two things are easy to miss. The web view holds its navigation delegate only weakly, so the composable must keep its own reference, or links silently stop working. And the update block runs more often than expected, so it loads the string only when it differs from the last one.

The view is also made transparent, with its scroll view background cleared, so the theme colour behind it shows through while the page loads.

Walking through the function

The function keeps a position in the original text and a buffer for the result. For each match it reads the file, and only if that succeeds does it copy the text up to the match, followed by the new source attribute. At the end it appends whatever is left. A failed read is simply skipped, so the original attribute is copied along with the rest.

The media type is chosen from the file extension: PNG files are declared as PNG and everything else as JPEG, which matches the two formats in these pages. If your content mixes more formats, that line needs to know about them, because a wrong type can leave an image blank on one platform and not the other.

The function is a suspending function, since reading shared resources is asynchronous. That is why the conversion happens in a coroutine and not while the screen is being composed.

Theme changes and recomposition

While the page is being prepared the screen draws nothing for a moment. Once the inlined text is ready, the full document is built from it and from the current colours. Both the content and the dark mode flag are keys of the remembered value, so switching the system theme rebuilds the document without reading the files again.

The iOS view then compares the new string with the last one it loaded; the Android wrapper in this app has no such check. Without that comparison, any unrelated recomposition could reload the page and throw the reader back to the top, which is the sort of bug that only appears when someone is halfway down a long page.

Testing without a phone

My store screenshots are produced by a test that runs on the computer. There the page is rendered with a base element that points at the resource folder on disk, so the same page builder can be used without inlining. That works because the test knows the real path.

That is exactly the difference between the two situations. When you control the file location, a base address is fine. Inside an installed app on two platforms, a self contained string removes a whole class of path problems.

Frequently asked questions

What is a data URI?

An address that contains the data itself, written as data, a media type, and usually base64 text. The browser needs no separate file.

Does inlining slow the page down?

For small images the effect is small. For large photographs the encoding and the longer string can be noticeable.

Do I need JavaScript for this?

No. The HTML is prepared in Kotlin before it reaches the web view.

Can the same Kotlin code serve both platforms?

The preparation can. Only the small view wrapper differs: WebView on Android and WKWebView on iOS.

More from the blog