Android stores and loads local files (external storage devices) and android storage devices
Sometimes the application needs to write data to the external storage of the device. If you want to share music, images, or online data with other apps or users, it is easier to share data stored on external devices. In addition, external devices usually have larger storage space.
You can use android. OS. Environment. getExternalStorageDirectory () to obtain the path of the sdCard. Create a MyFiles file in this path and save the data in the MyFiles folder.
The following shows how to store and load local files on an external storage device:
1. Create a project named DataStorage
2. Prepare the layout file (activity_data_storage.xml)
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: id = "@ + id/data_view" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "not operated"/> <Button android: id = "@ + id/save_button" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "save data"/> <Button android: id = "@ + id/load_button" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "loading data"/> </LinearLayout>
3. Add the permission to write files to sdCard in AndroidManifest. xml.
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
4. DataStorageActivity. java
Package com. example. datastorage; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStreamReader; import java. io. outputStreamWriter; import java. io. writer; import android. OS. bundle; import android. OS. environment; import android. support. v7.app. actionBarActivity; Import android. util. log; import android. view. view; import android. widget. button; import android. widget. textView; import android. widget. toast; public class DataStorageActivity extends ActionBarActivity {private static final String FILENAME = "data.txt"; private static final String TAG = "DataStorageActivity"; private TextView dataView; private Button saveButton; private Button loadButton; @ Overrideprotected Void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_date_storage); dataView = (TextView) findViewById (R. id. data_view); saveButton = (Button) findViewById (R. id. save_button); loadButton = (Button) findViewById (R. id. load_button); setListener ();} private void setListener () {saveButton. setOnClickListener (new View. onClickListener () {public void onClic K (View v) {try {saveData ();} catch (IOException e) {} Toast. makeText (DataStorageActivity. this, "saved successfully", Toast. LENGTH_SHORT ). show () ;}}); loadButton. setOnClickListener (new View. onClickListener () {public void onClick (View v) {try {loadData ();} catch (FileNotFoundException e) {} catch (IOException e ){}}});} public void saveData () throws IOException {File sdCard = Environment. getExternalStorageDirectory (); // Obtain the path Log of the external storage device (SD card. I (TAG, sdCard. getAbsolutePath (); // check LogCat. The absolute path of the SD card is/storage/sdcardsdCard = new File (sdCard, "/MyFiles"); sdCard. mkdirs (); // create the MyFiles directory (Multi-Level directory can be created) sdCard = new File (sdCard, FILENAME); FileOutputStream out = new FileOutputStream (sdCard ); writer writer = new OutputStreamWriter (out); try {String str = "data from internal storage devices"; writer. write (str);} finally {writer. close () ;}} public void lo AdData () throws FileNotFoundException, IOException {BufferedReader reader = null; StringBuilder data = new StringBuilder (); try {File sdCard = Environment. getExternalStorageDirectory (); sdCard = new File (sdCard, "/MyFiles/" + FILENAME); FileInputStream in = new FileInputStream (sdCard ); reader = new BufferedReader (new InputStreamReader (in); String line = new String (); while (line = reader. readLine ())! = Null) {data. append (line);} dataView. setText (data);} catch (FileNotFoundException e) {dataView. setText ("No stored data found");} finally {reader. close ();}}}
Run the program and click "save data". After the Toast is successfully saved. Click the load data button again:
It can be found that the device stored in the internal storage device is loaded and displayed in TextView. Check the file location again:
# DONE #
In Android, how does one obtain the "local storage" and "External Storage" paths?
External Storage path (sdcard): Environment. getExternalStorageDirectory (). getPath ();
All directories can be listed:
Environment. getRootDirectory (). getParent ()
Why does the Android phone prompt that no external storage is available?
You just need to buy a card and put it in it. The mobile phone storage is not big and it is used to install software, not to store photos and other music for you.