79413738

Date: 2025-02-05 05:38:35
Score: 0.5
Natty:
Report link

Here's the final solution from the github discussions

# Cargo.toml
dioxus = "0.6"
jni = "0.21"
#[cfg(target_os = "android")]
fn internal_storage_dir() -> anyhow::Result<PathBuf> {
    use jni::objects::{JObject, JString};
    use jni::JNIEnv;

    let (tx, rx) = std::sync::mpsc::channel();

    fn run(env: &mut JNIEnv<'_>, activity: &JObject<'_>) -> anyhow::Result<PathBuf> {
        let files_dir = env
            .call_method(activity, "getFilesDir", "()Ljava/io/File;", &[])?
            .l()?;
        let files_dir: JString<'_> = env
            .call_method(files_dir, "getAbsolutePath", "()Ljava/lang/String;", &[])?
            .l()?
            .into();
        let files_dir: String = env.get_string(&files_dir)?.into();
        Ok(PathBuf::from(files_dir))
    }

    dioxus::mobile::wry::prelude::dispatch(move |env, activity, _webview| {
        tx.send(run(env, activity)).unwrap()
    });

    rx.recv().unwrap()
}
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
Posted by: imbolc