Objective:
In our app development process basically will use the file storage, so file storage for us is quite familiar with, but since the release of Android 6.0, access to external sdcard based on the runtime permissions mechanism is required to dynamically request permission, So the previous direct SDcard root directory directly to create a new xxx/cache/
directory to do file storage is not so easy to control, it is necessary to re-understand the Android file storage knowledge.
Background:
Read and Write permissions for external SDcard
<android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>
Before Android 6.0, as long as the above two rights can be assured that the above permission to read and write on the SDcard, unfortunately, Android 6.0 after the need to dynamically request read and write permission, so we how to adapt to the Android 6.0来 file storage. First, we try to do the following when using file storage:
Do not arbitrarily occupy the user's built-in storage
Do not arbitrarily create a new directory on the SD card, you should place your app package name corresponding to the extended storage directory, uninstall the app can be automatically cleared.
There is an upper limit on the amount of disk space occupied, and according to certain policies to clear, such as DISKLRU algorithm.
Android File storage directory: 1.)
ApplicationPrivate storage (built-in storage)
Access mode |
Detailed path |
Whether you need permission to apply |
Context.getfiledir (); Gets the file directory under the built-in storage that can be used to save sensitive data that cannot be exposed to other applications, such as user personal information |
/data/data/App Package name/files/ |
Whether |
Context.getcachedir (); Gets the cache directory under the built-in storage, which can be used to save some cache files and automatically purge the system when there is insufficient space in the built-in storage |
/data/data/App Package name/cache/ |
Whether |
Attention:
Since the use of Android phone built-in storage, if the phone does not have root privileges, the file browser is inaccessible, the same storage will be deleted with the app deleted.
2.)
apply Extended Storage (SD card)
Access mode |
Path details |
Whether you need permission to apply |
Context.getExternalFilesDir() ;获取SD卡上的文件目录,
|
sdcard/android/data/App Package name/files/ |
API < 19: Yes API >= 19: No |
Context.getExternalCacheDir() ;获取SD卡上的缓存目录,可以用来保存一些缓存文件片
|
sdcard/android/data/App Package name/cache/ |
API < 19: Yes API >= 19: No |
Attention:
Because it is stored on the SDcard, as far as possible to avoid the existence of sensitive data such as user information, the file will be deleted as the app is deleted.
3.) Public Storage (SD card)
Access mode |
Path details |
Whether you need permission to apply |
Environment.getexternalstoragedirectory (); Get sdcard root directory |
Sdcard/xxx folder name/ |
Is |
Attention:
Sometimes we also need to store some common files, and we want these files to be deleted without the app being deleted, such as our recorded video or downloaded music. Since this directory can be accessed by any app, we need to apply for permission when we use it.
compatible with Android 6.0 file cache implementation
premise: Here mainly to deal with some non-permanent data, the need to permanently save the data as far as possible or to choose SDcard Public storage mode.
1.) Get the cache root directory
/*** Get the app's root directory * *@returnfile Cache root Path*/ Public StaticString Getdiskcacherootdir () {File diskrootfile; if(Existssdcard ()) {Diskrootfile=Leeapplication.getapp (). Getexternalcachedir (); } Else{diskrootfile=Leeapplication.getapp (). Getcachedir (); } String CachePath; if(Diskrootfile! =NULL) {CachePath=Diskrootfile.getpath (); } Else { Throw NewIllegalArgumentException ("Disk is invalid"); } returnCachePath; }
Here we need to determine if SDcard is available.
/** * Determine if the external sdcard can be used normally * @return **/public Static Boolean Existssdcard () { return Environment.MEDIA_MOUNTED.equals ( Environment.getexternalstoragestate ()) | | ! environment.isexternalstorageremovable (); }
2.) Get the directory of the specified function
/*** Get the relevant function business directory * *@returnFile Cache Path*/ Public Staticstring Getdiskcachedir (String dirName) {string dir= String.Format ("%s/%s/", Getdiskcacherootdir (), dirName); File File=NewFile (dir); if(!file.exists ()) { BooleanIssuccess =File.mkdirs (); if(issuccess) {log.d (TAG,"Dir mkdirs Success"); } } returnFile.getpath (); }
In the daily development process, we need to put different files in different directories, such as: Log log files need to be placed under the log file, you can pass the above method to the "log" to get the business function of the folder.
3.) Gets the specified function file path
/*** Get log log root directory *@return */ Public StaticString Getlogdir () {returnGetdiskcachedir (LOG); } /*** Get log file full path according to LogName *@paramLogName *@return */ Public Staticstring Getlogfilepath (String logName) {returnGetlogdir () +LogName; }
Summarize:
Today summarizes the Android file storage and on the 6.0 adaptation problem, recently suddenly feel that they have too much knowledge to learn, perhaps because of the current economic environment caused the Internet to meet the capital of winter, in fact, for me in 2012 has experienced once, for technical personnel should calmly look at this problem, need to do is to sow hope in the winter, that is to learn. In order to encourage the ~
Thinking of the file storage under Android 6.0 runtime permissions for Android data store