Android & lt; Data Storage (3) External Storage.. & gt;, androidstorage

Source: Internet
Author: User
Tags file url

Android <data Storage (3) External Storage. ..>, androidstorage

External Storage stores store data files on the SD card.

1. When saving data, you must first judge the SD card status.

Environment. getExternalStorageState () can be used to display the running status of the SD card.

There are two statuses:

MEDIA_UNKNOWN,MEDIA_REMOVED,MEDIA_UNMOUNTED,MEDIA_CHECKING,MEDIA_NOFS,MEDIA_MOUNTED,MEDIA_MOUNTED_READ_ONLY,MEDIA_SHARED,MEDIA_BAD_REMOVAL, OrMEDIA_UNMOUNTABLE.

We generally use

MEDIA_MOUNTED is operable

2. Obtain the directory of the SD card

You can use Environment. getExternalStorageDirectory () to obtain

3. Determine whether the path exists and perform the corresponding operations

Use file. getParentFile (). exists () to determine whether the file path exists. If it does not exist, create

File. getParentFile (). mkdir ();

4. Use outputstream, Writer, and printstream to write data.


5. The same method is used to read files.


XML file


<Span style = "font-size: 18px;"> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_vertical_margin" tools: context = ". mainActivity "> <Button android: id =" @ + id/button1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentTop =" true "android: layout_centerHorizontal = "true" android: layout_marginTop = "DP" android: text = "Save SD card files"/> <Button android: id = "@ + id/button2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerHorizontal = "true" android: layout_centerVertical = "true" android: text = "reading SD card files"/> </RelativeLayout> </span>


JAVA files

<Span style = "font-size: 18px;"> package com. example. externalstorage; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. outputStream; import java. io. printStream; import java. util. permission; import android. OS. bundle; import android. OS. environment; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. button; impo Rt android. widget. toast; public class MainActivity extends Activity {private Button saveData, getData; public static final String FILENAME = "flyou. dat "; // file name public static final String FILEDIR =" myFile "; // file path private PrintStream print under the SD card; // initialize the print stream private writable input; // initialize the input stream @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. Activity_main); // instantiate the Button saveData = (Button) this. findViewById (R. id. button1); getData = (Button) this. findViewById (R. id. button2); // store the file to the SD card saveData. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // obtain the SD card status and determine whether the String state = Environment is available. getExternalStorageState (); if (Environment. MEDIA_MOUNTED.equals (state) {// obtain the SD card and directory String SDpath = Environment. getExternalStorageDirectory (). toString (); // the complete File URL String path = SDpath + File. separator + FILEDIR + File. separator + FILENAME; // System. out. println (path); // obtain the File object File = new file (path); // automatically creates if (! File. getParentFile (). exists () {file. getParentFile (). mkdir ();} try {// use the print stream to redirect the write location, write data to the file print = new PrintStream (file); print. print ("this is a data file accessed on SDka using a print stream"); print. println ("\ r \ n second row of Data"); // in the file, the default "\ r \ n" is a line break} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {// close the print stream Toast. makeText (MainActivity. this, "file saved successfully, file location:" + path, 2 ). show (); print. close () ;}}}); // read the file getData. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubString state = Environment. getExternalStorageState (); if (Environment. MEDIA_MOUNTED.equals (state) {String SDpath = Environment. getExternalStorageDirectory (). toString (); String path = SDpath + File. separator + FILEDIR + File. separator + FILENAME; File file = new File (path); try {// Create a token to rewrite the output address to the file input = new partition (File ); // determine whether the next row of data exists while (input. hasNext () {String info = (String) input. next (); // The data Toast read Based on the display of the Toast. makeText (MainActivity. this, info, 2 ). show () ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {input. close () ;}}}) ;}</span>

Finally, you must Configure permissions for the android project.

Android. permission. WRITE_EXTERNAL_STORAGE






Export the file and use NotePad to open it.


NOTE:

If you only want to read files, you must configure android. permission. READ_EXTERNAL_STORAGE.

If the read operation is performed simultaneously, you only need to configure android. permission. WRITE_EXTERNAL_STORAGE.


Next prediction: Sqlite database storage




Data storage problems in Android Development

First of all, let me talk about it more. Let's take two steps first.
1. Network Access
2. Data Storage
I. Android Network Access is generally an imitation of the Http protocol. Simply put, it is the application of the HttpGet class and the HttpPost class. Then you give the address to it, and the http protocol will return a response, this response includes the return code and content, and the content you want is included in the returned content. So here is your first question: file or data? What is actually returned depends on what the server interface returns to you. Generally, an enterprise returns a string data in xml format or a string data in Json format, the content of your questions will be included in this article. Another possibility is that you initially accessed not a server interface, but a network file address. If so, if you want to obtain it, you need to use the JAVA input/output stream technology to read and write files on the network. Then, it is a file.
II. Data Storage
There are a total of five types of data storage for android. You can check them for details, but here I will mention that if you want to close the data, you should create a database, then insert the data into the database. If it is a file, you need to convert the binary data and store it again, so that it becomes confidential. If you want to put the data in your mobile phone card, so we need to have the first permission here, because you are a process from scratch, you have to create it without this file, and then write the file data to the file using the JAVA input/output stream technology, after the storage is complete or after writing, the entire operation is complete.
Please cherish it and contact me again if you do not understand it.

How many data storage formats does Android support? Or provide the address explained by the system.

1. SharedPreferences sharing preferences
2. Internal Storage space
3. External Storage
4. SQLite Database
5. Internet Network
Refer:
Blog.csdn.net/..950291
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.