Android外部存储 - 官方文档解读

预备知识External Storage Technical Information

摘要

"The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external storage on a device. Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions. Restricting writes in this way ensures the system can clean up files when applications are uninstalled."

"Starting in Android 4.4, multiple external storage devices are surfaced to developers through Context.getExternalFilesDirs(), Context.getExternalCacheDirs(), and Context.getObbDirs()."

 

Q&A

*Primary external storage? Secondary external storage?
-参考source.android.com/devices/tech/storage/config-example.html
有的手机不支持SD卡扩展(如Nexus 4),所以没有secondary;
有的手机支持(如Note 3),所以区分primary和secondary。
是否primary,由android/frameworks/base/core/res/res/xml/storage_list.xml决定。

一般地,primary是由内部存储(internal storage,比如NAND eMMC)中划分一部分为外部存储(external storage);mountPoint为"/storage/sdcard0";subsystem为"fuse",也就是由FUSE daemon模拟的。
secondary是可插拔(removable)的SD卡,mountPoint为"/storage/sdcard1"或"/storage/extSdCard";subsystem为"sd"。

 

*什么是package-specific directories?
-如package名为com.app.foo在primary/secondary的Android/data/com.app.foo目录。
/Android/data目录是由android/system/core/sdcard.c::handle_mkdir被调用时创建的,并同时新建.nomedia文件。
Context.getExternalFilesDirs(), Context.getExternalCacheDirs()会触发创建package-specific directories。
而Context.getObbDirs()会触发创建/Android/obb/。

 

*app对于SD卡的write权限范围分别在哪儿?
-在Note3上试验:
1.不声明WRITE_EXTERNAL_STORAGE情况下,不管是primary还是secondary,在package目录下能够写文件,在Android/data/下都会抛出IOException;
2.声明后,primary能够在Android/data/目录下写文件,而secondary依然IOException。
"For example, the app with package name com.example.foo can now freely access Android/data/com.example.foo/ on external storage devices with no permissions."
"Specifically, configuration and log files should only be stored on internal storage where they can be effectively protected."

 

相关讨论:
为什么 Android 4.4 KitKat 系统下第三方应用的 SD 卡读写权限受到了限制?

Android外部存储 - 官方文档解读,布布扣,bubuko.com

Android外部存储 - 官方文档解读

上一篇:Preventing CSRF in Java web apps---reference


下一篇:以Flappy Bird破解为例介绍andriod apk加壳方法