Android: disambiguating file paths -
in app, users pick files. internally, store information file, key based on file path. next time file used, stuff stored information. trouble instantiate files with:
file file1 = new file(environment.getexternalstoragedirectory() + "/test.txt");
and then, on particular jb device, file1.getcanonicalpath() gives: "/storage/emulated/0/test.txt".
the trouble when other apps launch app file path in intent, paths send tend like: "/mnt/sdcard/test.txt".
is there smart strategy disambiguate these 2 paths? possibly should instantiating files differently?
edit:
the trouble is, 2 canaonical paths 2 files not equal. below, cp1=="mnt/sdcard/test/txt"
, cp2=="/storage/emulated/0/text/txt"
:
file file1 = new file("/mnt/sdcard/test.txt"); file file2 = new file("/storage/emulated/0/test.txt"); string cp1 = file1.getcanonicalpath(); string cp2 = file2.getcanonicalpath();
first, correct way external path using getexternalstoragedirectory
, other getexternalstoragexxx
in android.
android firstly try resolve 2 system variable:
string rawexternalstorage = system.getenv(env_external_storage); string rawemulatedstoragetarget = system.getenv(env_emulated_storage_target);
while env_external_storage = "external_storage"
, env_emulated_storage_target = "emulated_storage_target"
. if emulated_storage_target
variable set, means device has emulated storage storage path emulated_storage_target
.(after android 4.2, support multiple-user external storage, there /0 or 0 after path) if not set , external_storage
set, path external_storage
. if both of them not set, path /storage/sdcard0
default. different devices may contain different paths external storage.
as external storage technical information says, can customize storage of device setting init.rc file. example in default goldfish one:
export external_storage /mnt/sdcard mkdir /mnt/sdcard 0000 system system symlink /mnt/sdcard /sdcard
if use getexternalstoragedirectory
/mnt/sdcard
, /sdcard
symbolic link directory.
so in case, init.rc may contain:
export emulated_storage_target /storage/emulated symlink /storage/emulated/0 /mnt/sdcard
so not ambiguous, same.
i think getcanonicalpath() might work vast majority of use cases.
a canonical pathname both absolute , unique. precise definition of canonical form system-dependent. method first converts pathname absolute form if necessary, if invoking getabsolutepath() method, , maps unique form in system-dependent way. typically involves removing redundant names such "." , ".." pathname, resolving symbolic links (on unix platforms), , converting drive letters standard case (on microsoft windows platforms).
every pathname denotes existing file or directory has unique canonical form. every pathname denotes nonexistent file or directory has unique canonical form. canonical form of pathname of nonexistent file or directory may different canonical form of same pathname after file or directory created. similarly, canonical form of pathname of existing file or directory may different canonical form of same pathname after file or directory deleted.
Comments
Post a Comment