How to store data under Android (ii)

Source: Internet
Author: User

In the previous article, we described how sharedpreferences is used.

Today we continue to introduce another way of storing data: using internal storage and external storage

Each Android device has two data storage areas: external storage and external storage

So what kind of storage should we use?

Let's compare the differences between the two ways of storage:

Internal storage:

  • is always available
  • The files saved here can only be accessed by your app.
  • When the program is uninstalled, all files belonging to the app will be deleted.

    Internal storage is the best choice when you don't want your files to be accessed by other people or apps

  • External storage:

  • When the SD card is uninstalled by the user is not available, so may need to do additional detection action
  • External storage can be read by everyone or app, with poor security
  • When the app is uninstalled, only files saved on the path obtained through Getexternalfilesdir () are deleted

    External storage is the best option when storage space is scarce and file access does not need to be limited

  • Save files to internal storage

    The operation of the file is mainly used in the Java IO Package class.

    To get the storage space for your program, you can use the following two methods:

    Getfilesdir ()

    Returns the internal storage path of a file type

    Getcachedir ()

    Returns the internal temporary storage path for a file type

    Let's look at the effect of the operation:

    When you are finished running to Eclipse's DDMS view, you can see that the file has been written:

    Detailed code:

    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 editor final Sharedpreferences.editor editor = Spf.edit (); Btnwrite.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {String filename = "Person.txt";//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 ()); O Penfileoutput.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 appropriate permissions to be obtained in the Androidmanifest file.

    <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

    When your program obtains permissions to write external storage, it also implicitly gets read permissions, so you do not need to add additional Read permissions.

    Operation Result:

    Program code:

    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 data to be written to string username = Namefield.gettext (). ToString (); String age = Agefield.gettext (). toString ();//Determine if the SD card is mounted and the SD card exists to begin writing to 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 not Present", Toast.length_short). Show ();}});}}

    File Save path:

    How to store data under Android (ii)

    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.