I am developing a Tauri application for Android where I need to display images from the device's local storage within a <img> tag. To achieve this securely and in a cross-platform manner, I am attempting to utilize a custom URL scheme, shiki://, registered via register_asynchronous_uri_scheme_protocol('shiki', handle_shiki_protocol) in my Rust backend.
My frontend uses an <img> tag with a src attribute structured as follows:
/* usage example */
<img src={"`shiki://image/content://com.android.externalstorage.documents/tree/primary/Pictures/Wallpapers/
some_image_file.jpg`"} />
My tauri.conf.json includes the following CSP configuration, which should permit the shiki: scheme for images
{
"security": {
"csp": {
"default-src": "'self' ipc: http://ipc.localhost",
"img-src": "'self' shiki: http://shiki.localhost blob: data:"
}
}
}
Expected Behavior:
- When the
<img>tag attempts to load theshiki://URL, Tauri'sregister_asynchronous_uri_scheme_protocol("shiki", handle_shiki_protocol)should be triggered. - Inside
handle_shiki_protocol(my Rust handler), I intend to parse the nestedcontent:// URI.. Through the Rust-Kotlin bridge, I plan to resolve the content:// URI using Android's ContentResolver to read the image data. - Finally, the image data should be forwarded back from the Rust handler to the JavaScript frontend as a ResponseBuilder's body.
Observed Issue:
The handle_shiki_protocol function is not being triggered when the <img> tag attempts to load the shiki:// URL on Android. This suggests that the WebView is either not recognizing the custom scheme, or there's an underlying issue with how it's attempting to resolve the URL before it reaches my custom protocol handler.
I have access to the URI of files listed in the Pictures/Wallpapers folder.
My questions are:
- Is there any mistake in the way I'm setting up my configuration?
- Is there any better way to do this?