How can I get the path of Android internal storage in [email protected]? #3475
-
I'm guessing I should use Crosslinks: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
I came across a similar issue today (finding some local directory where to store some persistent files) and came up with something like this. Disclaimer: I'm a total newbie when it comes to android development so I don't have much of a clue whether this is reasonable or not and how it compares to using fn get_cache_dir() -> Result<String> {
let ctx = ndk_context::android_context();
let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }?;
let mut env = vm.attach_current_thread()?;
let ctx = unsafe { jni::objects::JObject::from_raw(ctx.context().cast()) };
let cache_dir = env
.call_method(ctx, "getFilesDir", "()Ljava/io/File;", &[])?
.l()?;
let cache_dir: jni::objects::JString = env
.call_method(&cache_dir, "toString", "()Ljava/lang/String;", &[])?
.l()?
.try_into()?;
let cache_dir = env.get_string(&cache_dir)?;
let cache_dir = cache_dir.to_str()?;
Ok(cache_dir.to_string())
} |
Beta Was this translation helpful? Give feedback.
-
I had the same issue, here's my solution. I used #[cfg(target_os = "android")]
fn app_local_data_dir() -> Result<PathBuf> {
use jni::objects::{JObject, JString};
use jni::JNIEnv;
let (tx, rx) = std::sync::mpsc::channel();
fn run(env: &mut JNIEnv<'_>, activity: &JObject<'_>) -> 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))
}
wry::prelude::dispatch(move |env, activity, _webview| tx.send(run(env, activity)).unwrap());
rx.recv().unwrap()
} |
Beta Was this translation helpful? Give feedback.
I came across a similar issue today (finding some local directory where to store some persistent files) and came up with something like this. Disclaimer: I'm a total newbie when it comes to android development so I don't have much of a clue whether this is reasonable or not and how it compares to using
internal_data_path
.