As a developer, we often need to cache some files to the SD card, the common way is through:
File sdcard = Environment.getexternalstoragedirectory ();
Get the SD card root directory, and then customize the file/filename for file storage. The result of this is that when the phone has a large number of apps installed,
The SD card root directory can quickly become cluttered. And after API 6.0, the root file store requires user authorization, even if you
Androidmanifest.xml is configured in the storage permissions, the user is not authorized to write.
SD card Read and Write permissions:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>
In fact, Google has provided the best external storage solution, that is, the unified path is:
/android/data/< Package Name >/files/... (The path is usually mounted under/mnt/sdcard/)
External storage Path Invocation method is: Context.getexternalfilesdir (dir). GetAbsolutePath ()//through context call,
The parameter dir is a self-defined folder.
This method obtains the file storage path for the system after 6.0, the main androidmanifest.xml configuration read and Write permission,
There is no need for user re-authorization.
The above is an introduction to external storage, so if some phones do not have an SD card or the system itself does not allocate external storage space,
How should we cache the data?
That requires internal storage, the path to the internal storage is:/data/data/< Package name >/files/...
(This path is mounted on the phone's own storage directory)
The internal storage Path invocation method is: Context (). Getcachedir (). GetAbsolutePath ()//Call through context
Therefore, the way to get the storage path in normal development is:
/**
*
* @param Context Object
* @param dir Storage directory
* @return
*/
public static String GetFilePath (Context context,string dir) {
String directorypath= "";
Determine if the SD card is available
if (Media_mounted.equals (Environment.getexternalstoragestate ())) {
DirectoryPath =context.getexternalfilesdir (dir). GetAbsolutePath ();
DirectoryPath =context.getexternalcachedir (). GetAbsolutePath ();
}else{
No memory card to save the body memory
Directorypath=context.getfilesdir () +file.separator+dir;
Directorypath=context.getcachedir () +file.separator+dir;
}
File File = new file (directorypath);
if (!file.exists ()) {//Determine if the file directory exists
File.mkdirs ();
}
Logutil.i ("filepath====>" +directorypath);
return directorypath;
}
The difference between Getexternalcachedir () and Getcachedir () is the same as that of Getexternalfilesdir () and Getfilesdir (),
The former just automatically builds a Cach file directory under the path:/data/< package name >/files/cach/...
A concise schematic of storage differences is as follows:
Difference |
Method |
Note |
External storage External storage |
Environment.getexternalstoragedirectory () |
SD root directory:/mnt/sdcard/(6.0 after write requires user authorization) |
Context.getexternalfilesdir (dir) |
Path to:/mnt/sdcard/android/data/< package name >/files/... |
Context.getexternalcachedir () |
Path to:/mnt/sdcard//android/data/< package name >/cach/... |
Internal storage Internal storage |
Context.getfilesdir () |
Path is:/data/data/< package name >/files/... |
Context.getcachedir () |
Path is:/data/data/< package name >/cach/... |
Above, is the entire content of this article, if has the mistake and the description improper welcome everybody correct, thanks!