Storage options (Storage options)

Source: Internet
Author: User

I. sharepreferences: Storing private raw data in a key-value pair

1. Two ways to get shareprefereces objects

(1) getsharedpreferences (String name, int mode)

(2) getpreferences (int mode)

2. Steps to write values

(1) Call Edit to get the Sharepreferences.editor object

(2) Adding a key-value pair using a method such as Putboolean (String key, Boolean value)

(3) Call commit () save

3. Read the value

By means of shareprefereces Getboolean (String key, Boolean defvalue) , etc.

eg

public class Calc extends Activity {public    static final String prefs_name = "Myprefsfile";    @Override    protected void onCreate (Bundle state) {       super.oncreate (state);       . . .       Restore Preferences       sharedpreferences settings = getsharedpreferences (prefs_name, 0);       Boolean silent = Settings.getboolean ("Silentmode", false);       Setsilent (silent);    }    @Override    protected void OnStop () {       super.onstop ();      We need an Editor object to make preference changes.      All objects is from Android.context.Context      sharedpreferences settings = getsharedpreferences (prefs_name, 0); C16/>sharedpreferences.editor Editor = Settings.edit ();      Editor.putboolean ("Silentmode", Msilentmode);      Commit the edits!      Editor.commit ();    }}

Two. Using internal memory

1. Create a file and write it to internal storage

(1) Call openfileoutput (String name, int mode), open a file (created without existing), return the FileOutputStream object

(2) Call write (byte[] buffer) to the file

(3) Call Close () to close the stream

String FILENAME = "Hello_file"; string string = "Hello world!"; FileOutputStream fos = openfileoutput (FILENAME, context.mode_private); Fos.write (String.getbytes ()); Fos.close ();

2. Read the file from the internal memory

(1) Call openfileinput (String name) to return the FileInputStream object

(2) Call Read (byte[] buffer, int byteoffset, int byteCount) reading stream

(3) Call Close () to close the stream

3. Some other useful methods

LOG.I (tag, Getcachedir (). GetPath ())  //data/data/package name/CACHESLOG.I (tag, Getfilesdir (). GetPath ())    //data/data /Package Name/files

  

  

Storage options (Storage options)

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.