Let's consider this question first:
Open the phone settings, choose App Management, select any app, then you will see two buttons, one is to clear the cache, and the other is to clear the data, then when we click Clear Cache when the data is erased? What data is erased when we click to clear the data? After reading this article I believe you will have the answer.
In the development of Android we often hear such a few concepts, memory, internal storage, external storage, many people often confuse these three things, so we will start today to elaborate on the three what is the matter?
Memory, we in English called memory, internal storage, we call Internalstorage, external storage we call Externalstorage, which in English will not produce ambiguity, but when we translate into Chinese, the first two are abbreviated as memory, so, mixed.
So what exactly is internal storage and what is external storage?
First we open DDMS, there is a file Explorer, as follows:
Here are three folders we need to pay attention to, one is data, one is MNT, one is storage, we will talk about these three folders below.
1. Internal storage
The Data folder is what we often call internal storage, when we open the Data folder (no root phone can not open the folder), there are two folders worth our attention, as follows:
A folder is the app folder, there is a folder is the Data folder, the app folder contains all of our installed app apk file, in fact, when we debug an app, you can see the console output of the content, there is a uploading ... Just upload our apk to this folder and upload it successfully before you start the installation. Another important folder is the Data folder, this folder is a few package names, after opening these package names we will see some of these files:
1.data/data/Package Name/shared_prefs
2.data/data/Package Name/databases
3.data/data/Package Name/files
4.data/data/Package Name/cache
If you open the data file, you should know what these folders are for, we use sharedpreferenced, the data persisted in the local, in fact, there is the XML file in this file, The database files inside our app are stored in the Databases folder, and our normal data is stored in files, the cache files are stored in the cache folder, and the files stored here are called internal storage.
2. External storage
External storage is our usual operation of the most, external storage is generally what we see above the storage folder, of course, may also be the Mnt folder, this different manufacturers may not be the same.
In general, there is a SDcard folder in the storage folder, the files in this folder are divided into two categories, one is the public directory, there is a private directory, there are nine categories of public directories, such as DCIM, download and other such systems for us to create a folder, The private directory is the Android folder, this folder opens after a data folder, open the Data folder, there are many package name folder.
Speaking of which, I think we should be able to figure out what internal storage is. What is external storage? Well, when we're clear, we'll see how to operate internal storage and external storage.
3. Operating Storage space
First, after the above analysis, we have already understood, what is internal storage, what is external storage, and where these two storage methods are stored separately, generally speaking, we do not operate internal storage space, without root, we can not operate internal storage space, In fact, internal storage is mainly maintained by the system. However, in the code we are able to access this folder. Due to the limited internal storage space, we generally operate external storage space in the development, Google recommends that our app's data should be stored in the external stored private directory under the app's package name, so that when the user uninstalls the app, the relevant data will be deleted, if you directly in the/ Storage/sdcard directory creates an app folder, then when you delete the app, the folder will not be deleted.
After the above introduction, we can summarize the following table:
At a glance, what is internal storage and what is external storage.
If according to the characteristics of the path, we can also divide the path of the file storage into two categories, one is the path contains the package name, one is the path does not contain the package name, the path contains the package name, because it is related to an app, so access to these folders is called in the context of the method, without the path of the package name , which is not related to an app, we can access it by means of environment. Such as:
As you can see, the path of the package name we are all called in the context of the method to obtain, without the path of the package name, we directly call environment in the method obtained, then there are two methods need to pass in a string type of argument, This parameter we use the constant in the environment, the parameter means that we want to access which folder under this path, such as the Getexternalfilesdir method, we look at its source code:
/** * * @param type the type of files directory to return. May is null for * The root of the files directory or one of the * the following environment constants for a Subdirecto Ry: * {@link Android.os.environment#directory_music}, * {@link android.os.environment#directory_podcasts}, * { @link Android.os.environment#directory_ringtones}, * {@link android.os.environment#directory_alarms}, * {@link and Roid.os.environment#directory_notifications}, * {@link android.os.environment#directory_pictures}, or * {@link and Roid.os.environment#directory_movies}. * * @return The path of the directory holding application files * on external storage. Returns NULL if external storage is not currently * mounted so it could not ensure the path exists; You'll need to call * This method again if it is available. * * @see #getFilesDir * @see android.os.environment#getexternalstoragepublicdirectory * * @Nullable publi c ABstract File getexternalfilesdir (@Nullable String type); It has a lot of comments, and I'll just list some of them, and we'll see that we can access the music folder, movies folder, and so on in the Files folder.
Speaking of which, I think we have a clear understanding of internal storage, external storage. In development, we do not recommend writing too much data into internal storage, after all, space is limited. External storage is best used to store files in a private directory, which facilitates system maintenance and avoids user antipathy.
Now let's take a look at the questions we started with when we clicked to erase the data. There is no doubt that the files in the corresponding file and cache folders in the internal storage directory and the corresponding files and caches in the external storage folder, as well as the paths to these folders I think you should have understood.
All right, here we go. A file manipulation tool class:
public class Sdcardhelper {//Determine if the SD card is mounted public static Boolean issdcardmounted () {//return Environment.getexternalstoragestate (). Equals ("mounted"), return Environment.getexternalstoragestate (). Equals ( environment.media_mounted);} Get the root directory of the SD card public static String Getsdcardbasedir () {if (issdcardmounted ()) {return Environment.getexternalstoragedirectory (). GetAbsolutePath ();} return null;} Get the full space size of the SD card, return mbpublic static long Getsdcardsize () {if (issdcardmounted ()) {StatFs fs = new StatFs (Getsdcardbasedir () ); Long Count = Fs.getblockcountlong (); Long size = Fs.getblocksizelong (); return count * size/1024/1024;} return 0;} Get the remaining space size of the SD card public static long Getsdcardfreesize () {if (issdcardmounted ()) {StatFs fs = new StatFs (Getsdcardbasedir ()) ; Long Count = Fs.getfreeblockslong (); Long size = Fs.getblocksizelong (); return count * size/1024/1024;} return 0;} Get the free space size of the SD card public static long Getsdcardavailablesize () {if (issdcardmounted ()) {StatFs fs = new StatFs (getsdcardbased IR ()); Long Count =Fs.getavailableblockslong (); Long size = Fs.getblocksizelong (); return count * size/1024/1024;} return 0;} Save file public static Boolean Savefiletosdcardpublicdir (byte[] data, String type,string fileName) to the SD card's common directory { Bufferedoutputstream BOS = null;if (issdcardmounted ()) {File File = Environment.getexternalstoragepublicdirectory (type try {bos = new Bufferedoutputstream (new FileOutputStream (file, fileName)); Bos.write (data); Bos.flush (); return true;} catch (Exception e) {e.printstacktrace ();} finally {try {bos.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} return false;} Save the file to the SD card's custom directory public static Boolean Savefiletosdcardcustomdir (byte[] data, String dir,string fileName) { Bufferedoutputstream BOS = null;if (issdcardmounted ()) {File File = new file (Getsdcardbasedir () + file.separator + dir); (!file.exists ()) {file.mkdirs ();//recursive creation of custom directories}try {bos = new Bufferedoutputstream (new FileOutputStream (new file, fileName)); Bos.write (data); Bos.flush (); return true;} catch (Exception e) {e.printstacktrace ();} finally {try {bos.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} return false;} Save the file to the private files directory of the SD card public static Boolean Savefiletosdcardprivatefilesdir (byte[] data,string type, String fileName, Context context) {Bufferedoutputstream bos = null;if (issdcardmounted ()) {File File = Context.getexternalfilesdir (type); try {bos = new Bufferedoutputstream (new FileOutputStream (file, fileName)); Bos.write (data); Bos.flush (); return true;} catch (Exception e) {e.printstacktrace ();} finally {try {bos.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} return false;} Save the file to the SD card's private cache directory public static Boolean Savefiletosdcardprivatecachedir (byte[] data,string fileName, context Context) {Bufferedoutputstream bos = null;if (issdcardmounted ()) {File File = Context.getexternalcachedir (); try {bos = new Bufferedoutputstream (New FileOutputStream (New file (file, fileName)); Bos.write (data); Bos.flush (); return true;} catch (Exception e) {e.printstacktrace ();} finally {try {bos.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} return false;} Save Bitmap image to SDcard's private cache directory public static Boolean Savebitmaptosdcardprivatecachedir (Bitmap bitmap,string fileName, Context context) {if (issdcardmounted ()) {Bufferedoutputstream BOS = null;//Gets the private cache cache directory File File = Context.getexterna Lcachedir (); try {bos = new Bufferedoutputstream (new FileOutputStream (file, filename)); if (filename! = null && (Filename.contains (". png") | | filename.contains (". PNG ")) {bitmap.compress (Bitmap.CompressFormat.PNG, N, BOS),} else {bitmap.compress (Bitmap.CompressFormat.JPEG, (BOS);} Bos.flush ();} catch (Exception e) {e.printstacktrace ();} finally {if (BOS! = null) {try {bos.close ();} catch (IOException e) {E.printsta Cktrace ();}}} return true;} else {return false;}} Get file public static byte[] Loadfilefr from SD cardOmsdcard (String filedir) {Bufferedinputstream bis = null; Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); try {bis = new Bufferedinputstream (new FileInputStream (New File (Filedir))); byte[] buffer = new BYTE[8 * 1024];int C = 0;while ((c = bis.read (buffer))! =-1) {baos.write (buffer, 0, c); Baos.flush ();} return Baos.tobytearray ();} catch (Exception e) {e.printstacktrace ();} finally {try {baos.close (); Bis.close ();} catch (IOException e) { E.printstacktrace ();}} return null;} Looking for a file in the specified directory from SDcard, return bitmappublic Bitmap loadbitmapfromsdcard (String filePath) {byte[] data = Loadfilefromsdcard ( FilePath); if (data = null) {Bitmap BM = bitmapfactory.decodebytearray (data, 0, data.length); if (BM! = NULL) {return BM;} }return null;} Get the path to the public directory of the SD card publicly static string Getsdcardpublicdir (String type) {return Environment.getexternalstoragepublicdirectory (Type). ToString ();} Get the path to the SD card private cache directory public static String Getsdcardprivatecachedir (context context) {return Context.getexternalcachedir (). GETABSOLutepath ();} Get the path of the SD card private files directory public static string Getsdcardprivatefilesdir (context context, String type) {return Context.getexternalfilesdir (Type). GetAbsolutePath ();} public static Boolean isfileexist (String filePath) {File File = new file (FilePath); return File.isfile ();} Delete the file from SDcard public static Boolean Removefilefromsdcard (String filePath) {File File = new file (FilePath); if (file.exist S ()) {try {file.delete (); return true;} catch (Exception e) {return false;}} else {return false;}}}
This article related notes and source code download http://download.csdn.net/detail/u012702547/9348985
Thorough understanding of internal storage and external storage in Android