Detailed explanation of reading and writing content in SD card for Android data persistence

This paper describes the method of reading and writing content in SD card for Android data persistence. Share with you for your reference, as follows:

The three methods mentioned in the previous article: openfileoutput and openfileinput can process the data in any file through the stream objects OutputStream and InputStream, but like SharedPreferences, files can only be created in the specified directory of the mobile phone memory. Therefore, they have great limitations in actual development and use. In this section, Let's look at a more advanced method to realize data persistence - reading and writing the content on the SD card.

-- read the files in the assets directory

The folder assets in Android stores binary file formats, such as audio, video, pictures, etc., but the files in this directory will not be indexed by r.java files. If you want to read the files in this directory, you also need to use assetmanager objects.

The code is as follows:

Starting from Android 2. X, writing files to the SD card is not allowed by default. Therefore, to add permissions, add the following code to the androidmanifest.xml file:

So where is this file saved? In eclipse, enter the file explorer panel, select the APK file of the program in the / data / APP directory, export it to the desktop or other places, unzip it and enter the assets directory to see the picture just saved.

Because the files in the assets folder are packaged into APK files, the files in the assets directory can only be read, not written.

-- Sax engine reads XML file

Principle:

The Android SDK itself provides a class library for operating XML files, which is Sax. Using Sax to process XML requires a handler object. Generally, a subclass of org.xml.sax.helpers.defaulthandler is used to create a handler object. When Sax technology processes XML files, it does not load XML files into memory at one time, but parse them while reading. Therefore, the following five analysis points (analysis events) are required:

1. Start analyzing the XML file: the corresponding method defaulthandler.startdocument can do some initialization work in this method

2. Start processing each XML tag, that is, the starting tag of each tag pair: the corresponding method startelement, which can obtain the name and attribute information of the current tag

3. After processing each XML tag, that is, the end tag of each tag pair: the corresponding method endelement obtains all the information of the currently processed tag

4. When you finish processing the XML file, that is, when you finish processing the contents of the entire XML file, this step is reached. The corresponding method is enddocument

5. Reading the character analysis point is to process all the contents of the above obtained XML file. This step is very important. The corresponding method: characters is used to process the contents of the obtained XML file, that is, save the contents of the XML tag.

The following is the application of the above five points to convert XML files into Java objects:

First, create a wxml file under / RES / raw:

Define a product class:

The following is the xml2product class, which is a subclass of defaulthandler. This class is the most important and core class in the whole program:

The following is the class that converts XML files into Java objects:

For more Android related content, readers who are interested can view the special topics of this site: summary of SD card operation methods for Android programming and development, summary of Android file operation skills, summary of Android database operation skills, summary of activity operation skills for Android programming, introduction and advanced tutorial for Android development, summary of Android resource operation skills Android view skills summary and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>