androidrustdioxus

How can I get the path of Android internal storage in Dioxus@0.6?


I'm guessing I should use android_activity::AndroidApp::internal_data_path. But how can I access access to AndroidApp in Dioxus?

Crosslinks:


Solution

  • 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()
    }