#Custom URL Scheme(Android): Not triggering register_asynchronous_uri_scheme_protocol("shiki", ...)

7 messages · Page 1 of 1 (latest)

worn stratus
#

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:

  1. When the <img> tag attempts to load the shiki:// URL, Tauri's register_asynchronous_uri_scheme_protocol("shiki", handle_shiki_protocol) should be triggered.
  2. Inside handle_shiki_protocol (my Rust handler), I intend to parse the nested content:// URI.. Through the Rust-Kotlin bridge, I plan to resolve the content:// URI using Android's ContentResolver to read the image data.
  3. 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:

  1. Is there any mistake in the way I'm setting up my configuration?
  2. Is there any better way to do this?
fast solstice
#

Plz, explain more detail

worn stratus
#

I've added more details within the character limit of the post, let me know if you could understand it

foggy canyon
#

When the <img> tag attempts to load the shiki:// URL, Tauri's
That should be the issue. On Android and Windows it's http://shiki.localhost

worn stratus
#

Thanks @foggy canyon, I tried with http://shiki.localhost and it did work as intended. Can you point me to a source which can help me understand why there was a need for such distinction for Windows and Android platform? I feel like I haven't fully understood this part and would like to imbue it in my brain

foggy canyon
#

there's not much to tell you, the android and windows webviews simply don't have an api that allows to register a custom scheme

#

they do have request listeners though (with more control than request listeners on the other platforms) which we can use with a imaginary domain on a http(s) protocol