Source: https://github.com/kymjs/KJFrameForAndroid
Use of the Environment class: http://www.2cto.com/kf/201408/327215.html
First, check whether the phone is inserted into the SD card
Step: ① through the environment class to get the phone status, determine whether the SD card exists
Knowledge Point: Environment class get SD card status
Android application development, often use the environment class to obtain external storage directory, before accessing external storage must first determine whether the external storage is already available (mounted &available) status, and you need to add external storage read and write permissions to the Androidmanifest.xml file. Several static constants are provided in the environment class to identify the state of the external storage, which are string types Media_bad_removal the storage media has been removed before it is mounted. Media_checking is checking storage media. media_mounted storage media is already mounted and the mount point is readable/write. Media_mounted_read_only storage media has been mounted, mount point is read only. Media_nofs storage Media is a blank or unsupported file system. media_removed storage media is removed. media_shared storage media is being shared via USB. media_unmountable storage media cannot be mounted. media_unmounted storage media is not mounted. The state of the external storage can be obtained through the static method Getexternalstoragestate (), if the program needs to read and write data in the external storage, it must first determine:if(Environment.MEDIA_MOUNTED.equals (Environment.getexternalstoragestate ())|| !environment.isexternalstorageremovable ()) Then, add the externally stored read and Write permissions:<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE" ><uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission></uses-permission>
can obtain the status parameters of SD card
Second, get the SD card path
Step: Call the Environment class to get the SD card path.
Knowledge Point: The file handle that the environment class can get
①, public folder handle (example: Camera shooting standard directory, film storage standard directory, etc.)
Called: environment.getexternalstoragepublicdirectory (type); the type class refers to the standard directory where the directory_alarms system reminds the ringtone to be stored. DIRECTORY_DCIM camera's standard catalog for taking photos and videos. Directory_downloads download the standard directory. Directory_movies the standard catalogue of movie storage. Directory_music Standard catalogue for music storage. Directory_notifications system notifies the standard directory where the ringtone is stored. The standard directory where the directory_pictures picture is stored directory_podcasts the system broadcast. Standard directory for directory_ringtones system ringtone storage.
handle to public folder
②, SD card path, system path and other paths
Static File Getdatadirectory () obtains the Data directory (/data). static File getdownloadcachedirectory () Gets the download cache directory. (/cache)static File getexternalstoragedirectory () obtains the external storage media directory. (/mnt/sdcard or/storage/sdcard0)static File getrootdirectory () obtains system home directory (/system)
ways to get individual paths
Aside: When using LRUCache and DISKCAHCE, it is necessary to use the cache folder (when the app uninstall is removed)
So the path to the cache folder:
Context.getcachedir () Get the application's own cache directory
Context.getexternalcachedir () Gets the storage directory where the application is stored externally
Third, get the handle of the desired folder
Steps: ①, gets the folder handle ②, determines whether the folder exists, returns an absolute path, or creates a folder if it does not exist.
Knowledge Points: The creation of file folders, the creation of file files, the meaning of what the file object itself represents.
Iv. getting the path to the file
Step: ①, first determine whether the folder on the file path exists ②, create a handle to the file ③, determine whether the file exists, if it does not exist, create the file ④, and finally return file
V. Convert InputStream stream to byte[] Array
Step: 1, determine whether the InputStream is not NULL 2, the InputStream set on butteredinputstream (improve efficiency) 3, create a byteoutputstream stream (InputStream data written in memory, Then take out) 4, write the InputStream data to the Byteoutputstream
Knowledge points: ①, flow of decorative categories and use
Vi. method of unifying closed flows
Step 1, set the input parameter to a variable input parameter: closeable ... closeables (closeable is the interface that closes the stream, the stream inherits the interface) 2, determines whether closeables is not NULL 3, Iterates through the closeable with a foreach loop and closes the stream.
Knowledge points: ①, stream inheritance Closeable interface, to close the stream native ②, foreach loop condition is to inherit the Iterable interface class or array
Vii. convert bitmap into a file
Step 1, determine if bitmap and file path is null 2, create file ()
What should the Fileutils class be