General re-access to the SD card before the path to obtain the SD card, to prevent different vendors have different path configuration. Android provides the environment class to get the system's current SD card path.
LOG.D (TAG, Environment.getexternalstoragedirectory (). GetAbsolutePath ());
In the case of Samsung mobile phones in hand, the results are printed
533 09-20 14:29:47.850 D 32105 32105 sdtest :/storage/emulated/0
ADB shell connects to the phone to confirm the next path
1 [email protected]:/proc/32105 # cat Mounts2cat mounts3... 4 /dev/block/platform/soc.2/by-name/user/data ext4 Rw,seclabel,nosuid,nodev,noatime,discard,journal_ Checksum,journal_asyn5 c_commit,noauto_da_alloc,data=ordered 0 06 ... 7 /data/media/storage/emulated/0 Sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023,derive=legacy, RESERVED=20MB 0 0
/storage/emulated/0 Directory is sdcardfs to the directory/data/media,/data directory is the system directory, thus
Environment.getexternalstoragedirectory ()
Get to the address of the internal SD card of the fuselage, not the address of the real external SD card. Android system is to distinguish between internal SD card and external SD card two kinds, the general internal SD card is a part of internal ROM storage space cut out, the meaning of logic is greater than physics.
When we want to access the SD card in the following way
1 New File (Environment.getexternalstoragedirectory (), "Text.txt"); 2 Try {3 sd.createnewfile (); 4 Catch (IOException e) {5 e.printstacktrace (); 6 }
Will find again CreateNewFile () when the program throws an IO exception, the system calls open when the return eacces file access permissions are wrong.
1036 09-20 14:46:28.850 W 32368 32368 System.err:java.io.IOException:open failed:eacces (Permission Denied)1037 09-20 14:46:28.850 W 32368 32368 System.err:at java.io.File.createNewFile (file.java:946)
W 32368 32368 system.err: caused By:libcore.io.ErrnoException:open failed:eacces (Permission denie
d
Because we don't have permission to use the declaration in Androidmanifest.xml.
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
Android access to the SD card directory is not restricted directly by controlling the Linux kernel file permissions rwx flag bit to restrict application access, but indirectly through the package Management Service control a file
/data/system/packages.list
To implement, we first look at the time when the Uses-permission declaration is not joined, what is the content of the file
/data/system/packages.list | Default None
The currently owned group permissions are none, and after adding the User-permission declaration, the contents of the file become
Cat/data/system/packages.list | Default 1028,1015
Added 1028 and 1015 groups of permissions.
And 1028 happens to be sdcard_r, thus indirectly has the access rights of the SD card.
Access to SD card