Data storage in Android (2): android Storage
In the previous article, we introduced how to use SharedPreferences.
Today, we will continue to introduce another way to store data: using internal storage and external storage.
Each Android device has two data storage areas: External Storage and external storage.
What storage method should we use?
Let's first compare the differences between the two storage methods:
Internal Storage:
Always available
Files saved here can only be accessed by your app
When the program is uninstalled, the system will delete all files belonging to the application.When you do not want your files to be accessed by others or applications, internal storage is the best option.
External Storage:
The SD card is not available when it is uninstalled by the user. Therefore, additional detection actions may be required.
External storage can be read by all users or applications, with poor security
When an application is uninstalled, it is only stored inGetExternalFilesDir ()The file on the obtained path is deleted.When there is a shortage of storage space and there is no need to restrict access to files, external storage is the best choice.
Save files to internal storage
File Operations mainly use the classes in the java I/O package.
To obtain the storage space of your program, you can use the following two methods:
GetFilesDir ()
Returns a File-type internal storage path.
GetCacheDir ()
Returns an internal temporary storage path of the File type.
Let's take a look at the running effect:
Package com. whathecode. storageoptinos; import java. io. fileOutputStream; import android. app. activity; import android. content. context; import android. content. sharedPreferences; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); final EditText namefield = (EditText) findViewById (R. id. username); final EditText agefield = (EditText) findViewById (R. id. age); Button btnWrite = (Button) findViewById (R. id. write); SharedPreferences spf = getSharedPreferences ("record", MODE_PRIVATE); // get the final SharedPreferences editor. editor editor = spf. edit (); btnWrite. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {String filename = "person.txt"; // File file File = new File (getBaseContext (). getFilesDir (), // filename); String username = namefield. getText (). toString (); String age = agefield. getText (). toString (); try {FileOutputStream openFileOutput = openFileOutput (filename, Context. MODE_PRIVATE); openFileOutput. write (username. getBytes (); openFileOutput. write (age. getBytes (); openFileOutput. close (); Toast. makeText (getBaseContext (), "file saved at" + getFilesDir (). toString (), Toast. LENGTH_LONG ). show ();} catch (Exception e) {e. printStackTrace ();}}});}}
Save files to external storage
Unlike internal storage, writing files to external storage requires the corresponding permissions in the AndroidManifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
When your program has the permission to write external storage and implicitly obtains the read permission, you do not need to add additional read permissions.
Running result:
Package com. whathecode. storageoptinos; import java. io. file; import java. io. fileOutputStream; import android. app. activity; import android. OS. bundle; import android. OS. environment; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); final EditText namefield = (EditText) findViewById (R. id. username); final EditText agefield = (EditText) findViewById (R. id. age); Button btnWrite = (Button) findViewById (R. id. write); btnWrite. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {String filename = "person.txt"; String externDir = getExternalFilesDir (null ). toString (); // prepare the data to be written to String username = namefield. getText (). toString (); String age = agefield. getText (). toString (); // determines whether the SD card is mounted. if the SD card exists, write the file if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {try {FileOutputStream fos = new FileOutputStream (new File (externDir, filename); fos. write (username. getBytes (); fos. write (age. getBytes (); fos. close (); Toast. makeText (getBaseContext (), "file saved at" + externDir, Toast. LENGTH_LONG ). show ();} catch (Exception e) {e. printStackTrace () ;}} else {Toast. makeText (getBaseContext (), "SD card does not exist", Toast. LENGTH_SHORT ). show ();}}});}}
File Storage path: