Android-SDCard Data Access & amp; Environment Introduction, androidsdcard access

Source: Internet
Author: User

Android-SDCard Data Access & Environment Introduction, androidsdcard access

1: Introduction to Environment:

Environment is android. A class under the OS package. Google officially explained it as: Provides access to environment variables (variables that provide the access environment). It can be seen that this class is a device category class used by programs to access SDCard.

The Environment constant construction method is as follows:

 

Resolution: for example, the field DIRECTORY_PICTURES is the name of the Pictures directory in SDcard, where SDCardPicturesName = "Pictures ";

 

String SDCardPicturesName=Environment.DIRECTORY_PICTURES;

For example, the Construction Method: getExternalStorageDirectory (); returns the primary external storage directory returned. That is, the root directory of the SDCard. As shown in:

 

 

2: Use Environment in a simple android projec project to read and write the SDCard file directory.

First, you need to obtain permissions to operate the data on the storage card. We configure the following in AndroidMainfest. xml:

<! -- Create and delete file permissions in SDCard --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> <! -- Write data permission to SDCard --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>

The test application interface is as follows. If you do not want to explain it, you can understand the intention as soon as you can:

 

The sample code is as follows:

1 package activity. cyq. helloandroidstudio; 2 3 import android. OS. environment; 4 import android. support. v7.app. appCompatActivity; 5 import android. OS. bundle; 6 import android. view. view; 7 import android. widget. editText; 8 import android. widget. textView; 9 import android. widget. toast; 10 11 import java. io. bufferedReader; 12 import java. io. file; 13 import java. io. fileInputStream; 14 import java. io. fil ENotFoundException; 15 import java. io. fileOutputStream; 16 import java. io. IOException; 17 import java. io. inputStreamReader; 18 import java. io. outputStreamWriter; 19 20 public class MainActivity extends AppCompatActivity {21 private EditText writeEdit; 22 private TextView showData; 23 private final String FILE_NAME = "MyCreate.txt"; 24 File sdcard = Environment. getExternalStoragePublicDirectory ("t Xt "); // defines the main external storage directory. 25 String file = Environment. getExternalStorageDirectory () + "/Pictures"; 26 File myfile = new File (sdcard, FILE_NAME); 27 File testFile = new File (file ); 28 29 @ Override 30 protected void onCreate (Bundle savedInstanceState) {31 super. onCreate (savedInstanceState); 32 setContentView (R. layout. activity_main); 33 34 writeEdit = (EditText) findViewById (R. id. write_edit); 35 showData = (TextView) findVi EwById (R. id. show_text); 36 37/* write data to the SDCard file */38 findViewById (R. id. write_btn ). setOnClickListener (new View. onClickListener () {39 @ Override 40 public void onClick (View v) {41 if (! Sdcard. exists () {42 Toast. makeText (MainActivity. this, "this device SDCard does not exist", Toast. LENGTH_SHORT ). show (); // determine whether the device has an sdcard 43 return; 44} 45 if (! Myfile. exists () {/* If the file does not exist, wear the file */46 try {47 myfile. createNewFile (); 48} catch (IOException e) {49 e. printStackTrace (); 50} 51} 52/* file write operations */53 try {54 FileOutputStream fos = new FileOutputStream (myfile); 55 OutputStreamWriter opw = new OutputStreamWriter (fos ); 56 opw. write (writeEdit. getText (). toString (); 57 opw. flush (); 58 opw. close (); 59 fos. close (); 60 Toast. makeText (MainActivity. this ," Data written successfully ", Toast. LENGTH_SHORT ). show (); 61} catch (FileNotFoundException e) {62 e. printStackTrace (); 63} catch (IOException e) {64 e. printStackTrace (); 65} 66 67} 68}); 69 70/* read the file data in the SDCard */71 findViewById (R. id. read_btn ). setOnClickListener (new View. onClickListener () {72 @ Override 73 public void onClick (View v) {74 if (! Sdcard. exists () {75 Toast. makeText (MainActivity. this, "this device SDCard does not exist", Toast. LENGTH_SHORT ). show (); // determine whether the device has sdcard 76 return; 77} 78 if (! Myfile. exists () {79 try {80 myfile. createNewFile (); 81} catch (IOException e) {82 e. printStackTrace (); 83} 84} 85 try {86 FileInputStream FCM = new FileInputStream (myfile); 87 InputStreamReader isr = new InputStreamReader (AIS ); 88 BufferedReader br = new BufferedReader (isr); 89 StringBuilder strBuilder = new StringBuilder (); 90 String line = ""; 91 while (line = br. readLine ())! = Null) {92 strBuilder. append (line); 93} 94 br. close (); 95 isr. close (); 96 fi. close (); 97 Toast. makeText (MainActivity. this, "data read succeeded", Toast. LENGTH_SHORT ). show (); 98 showData. setText (strBuilder); 99} catch (FileNotFoundException e) {100 e. printStackTrace (); 101} catch (IOException e) {102 e. printStackTrace (); 103} 104 105} 106}); 107 108 findViewById (R. id. text_btn ). setOnClickListener (new View. onClickListener () {109 @ Override110 public void onClick (View v) {111 if (testFile. exists () {112 Toast. makeText (MainActivity. this, "Pictures file exists", Toast. LENGTH_SHORT ). show (); 113 try {114 new File (testFile + "/testNew.txt "). createNewFile ();/* call the File constructor to create a File */115} catch (IOException e) {116 e. printStackTrace (); 117} 118} else {119 Toast. makeText (MainActivity. this, "Pictures file does not exist", Toast. LENGTH_SHORT ). show (); 120} 121} 122}); 123} 124}


Supplement: four construction methods of the File class:

File (File dir, String name );

File (String path );

File (String dirPath, String name;

File (URI uri );

 

 

 

 

 

 

 

 

 

 

 

 

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.