If you need to store data in your program using SDcard, you need to use the Androidmainfset.xml file
To configure the permissions:
Java code:
- <!--permissions to create and delete files in SD--
- <uses-permission
- Android:name="Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--Write permissions to the SD card--
Java code:
- <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>
Tips:
The knowledge points that will be used when reading and writing the SD card:
Static methods for the Environment class:
* Getdatadirectory (); Get the data directory in Android;
* Getexternalstorgedirectory (); Get to the external storage directory generally refers to the SD card;
* Getdownloadcachedirectory (); Gets the cache directory to download;
* Getexternalstoragestate (); Gets the current state of the external settings, SD card
Android for external settings state, we are more commonly used should be media_mounted (SD card exists and can be
Read and Write
* Getrootdirectory (); Get to the Android root path;
* isexternalstorageemulated (); Returns a Boolean value to determine whether the external setting is valid;
* Isexternalstorageremovable (); Determine if external settings can be removed;
The status of the external settings in Android is as follows:
* media_mounted SD card can read and write;
* Media_mounted_read_only SD card exists, can only be read operation
....................................................... [/b] [/color]
Case: effect implementation, click Implementation to write data to the SD card, the code is as follows:
Java code:
- /* Write the file data to SDcard, save the data */
- Ublic void Writedatatosd () {
- try{
- / * Get the file object and determine the information for the data file * /
- //file file = new file (environment.getexternalstoragedirectory () + "/f.txt");
- File File = new file (Environment.getexternalstoragedirectory (),"F.txt");
- /* Determine if the external settings of the SD can read/write */
- if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
- /* The object of the stream */* * *
- FileOutputStream fos = new FileOutputStream (file);
- / * Data that needs to be written * /
- String message = "The weather is not very good";
- / * Convert string to byte array * /
- byte[] buffer = message.getbytes ();
- / * Start writing data * /
- Fos.write (buffer);
- / * Turn off the use of the stream * /
- Fos.close ();
- Toast.maketext (mainactivity. This, "file write succeeded", " ()." Show ();
- }
- }catch (Exception ex) {
- Toast.maketext (mainactivity. This, "file write Failed", + ). Show ();
- }
Read the files in the SD card:
/* Read Data */
Java code:
- Public void Readdatafromsd () {
- try{
- / * Create a file object to identify the information that needs to be read * /
- File File = new file (Environment.getexternalstoragedirectory (),"F.txt");
- / * Fileinputsteam The input stream object, * /
- FileInputStream FIS = new FileInputStream (file);
- / * Prepare a byte array for the user to load the data that will be read * /
- byte[] buffer = new byte[fis.available ()];
- / * Start to read the file * /
- Fis.read (buffer);
- / * Close stream * /
- Fis.close ();
- / * Convert a byte array into a character A and convert the encoded format * /
- String res = encodingutils.getstring (buffer, "UTF-8");
- Toast.maketext (mainactivity. This, "The file read successfully, you read the data is:" +res, " ." Show ();
- }catch (Exception ex) {
- Toast.maketext (mainactivity. This, "file read failed!" ", + ). Show ();
- }
- }
-android use the file class to read data files in the SD card