Android stores and loads local files (internal storage devices) and android storage devices

Source: Internet
Author: User

Android stores and loads local files (internal storage devices) and android storage devices

All applications on Android devices have a directory of files placed in the sandbox. Saving the files in the sandbox can prevent other applications from accessing them.

The full path of the sandbox directory is/data/<package name> viewed in File Explorer:


As you can see, each application has a file directory named after this package under/data.

This article describes how to save files in the/data/<package name>/files/directory.


The following shows how to store and load local files on an internal 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. DataStorageActivity. java

Package com. example. datastorage; import java. io. bufferedReader; import java. io. fileNotFoundException; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java. io. outputStreamWriter; import java. io. writer; import android. content. context; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. util. log; imp Ort 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 savedI NstanceState) {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 onClick (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 {OutputStream out = this. openFileOutput (FILENAME, Context. MODE_PRIVATE);/* parameter 1: file name. * If the file does not exist, Android automatically creates it. The created file is saved in the/data/<package name>/files directory * parameter 2: file operation mode parameters. Indicates that the file is private data and can only be accessed by the application itself. **/Writer writer = new OutputStreamWriter (out); try {String str = "data from internal storage devices"; writer. write (str);} finally {writer. close () ;}} public void loadData () throws FileNotFoundException, IOException {BufferedReader reader = null; StringBuilder data = new StringBuilder (); try {InputStream in = this. openFileInput (FILENAME); Log. I (TAG, in. toString (); reader = new BufferedReader (new InputStreamReader (in); Str Ing 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 ()

What are the "internal storage space", "built-in storage card", and "external storage card" of Android phones?

The internal storage space is actually called RAM, which is the memory running on mobile phones. The larger the memory size, the better the memory size.
The built-in memory card is the rom that comes with the phone memory.
An external memory card is an SD card used to store files.

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.