Android access "/data/data/packageName/"folder
1848 ワード
Sometimes in android test we need to write to "/data/data/packageName/"folder .
When declaring this :
it shows :
but
So, after some searching , I found this :
Problem solved !
Context mContext = InstrumentationRegistry.getTargetContext();
File textFile = new File(mContext.getApplicationInfo().dataDir + File.separator + "textFile.txt");
if (textFile.createNewFile()){
Log.i(TAG, "testMethods: Create text file succeed .");
} else {
Log.w(TAG, "testMethods: Fail to create text file !!!");
}
When declaring this :
mContext.getApplicationInfo().dataDir
it shows :
mContext.getApplicationInfo().dataDir : /data/user/0/com.mrvl.mypackage
but
xxx@xxx:/data/data/com.mrvl.mypackage # ls -al
-rw------- u0_a52 u0_a52 0 2017-08-08 03:45 textFile.txt
So, after some searching , I found this :
/data/user was added in Jelly Bean as part of multi-user support.
Each user on the device gets a directory in there named after their user ID,
and that directory contains each app's data directory for that user. /data/user/0 is a symlink to /data/data.
A symbolic link, also termed a soft link, is a special kind of file that points to another file,
much like a shortcut in Windows or a Macintosh alias.
Unlike a hard link, a symbolic link does not contain the data in the target file.
It simply points to another entry somewhere in the file system.
This difference gives symbolic links certain qualities that hard links do not have,
such as the ability to link to directories,
or to files on remote computers networked through [NFS](https://kb.iu.edu/d/adux).
Also, when you delete a target file, symbolic links to that file become unusable,
whereas hard links preserve the contents of the file.
Problem solved !