Android Data Storage

Source: Internet
Author: User

Android Data Storage

Android provides multiple optional methods for you to permanently store application data. The selection of a solution depends on your special needs, such as whether the data needs to be private to your application or can be accessed by other applications (or users, and how much storage space you need.

Data storage solutions include the following:

Shared Preferences

Stores private raw data in the form of key-value pairs

Internal Storage

Store private data in the device's storage Zone

External Storage

Save shared data in the shared external storage area

SQLite Databases

Store structured data in a private database

Network Connection

Store data in your web server

Android also provides a way to expose private data to other applications-through content provider. content provider is an optional component for other applications to read and write your application data, depending on what data you want to expose. For more information about content Providers, see Content Providers.

Shared Preferences

You can use either of the following methods to obtain the Shared Preferences object of your application.

· GetSharedPreferences ()-If you need multiple preferences identified by names, you can use this method to specify the expected preferences using the first parameter.

· GetPreferences ()-If your Activity only needs a preferences file, you can use this method. The preferences file obtained by this method will be the only preferences in the Activity, and you do not need to provide a name.

Write value:

1. Call edit () t to obtain a SharedPreferences. Editor object.

2. Write values by using methods such as putBoolean () and putString.

3. Submit the modified data using the commit () method.

Read the value through SharedPreferences's getBoolean () and getString () methods.

The following is an example of SharedPreferences:

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 are from android. context. Context
SharedPreferences settings = getSharedPreferences (PREFS_NAME, 0 );
SharedPreferences. Editor editor = settings. edit ();
Editor. putBoolean ("silentMode", mSilentMode );

// Commit the edits!
Editor. commit ();
}
}

Internal Storage

You can directly save the file to the internal storage of the device. By default, files stored in the internal storage are private to your application, and other applications (or users) cannot access them. After the user uninstalls your program, these files will be deleted.

Create and write private files to internal storage:

1. Call openFileOutput () and input the file name and operation method. This method returns a FileOutputStream object.

2. write a file using the write () method.

3. close the stream using the close () method.

For example:

String FILENAME = "hello_file ";
String string = "hello world! ";

FileOutputStream fos = openFileOutput (FILENAME, Context. MODE_PRIVATE );
Fos. write (string. getBytes ());
Fos. close ();

In MODE_PRIVATE mode, a file is created (or an existing file with the same name is replaced) and private to your program. Other available modes include MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.

Read files from an internal Bucket:

1. Call openFileInput () and input the name of the file to be read. This method returns a FileInputStream.

2. read the byte read () from the file ().

3. close the stream close ().

Tip: If you want to save a static file in your application during compilation, you can store the file in the res/raw/directory. You can use openRawResource () to open the file and pass in R. raw. Resource ID. This method returns an InputStream. You can use this input stream to read your file (but cannot write it to the original file ).

Save cache files

If you want to cache some data instead of permanently saving them, you should use the getCacheDir () method to open a File object that represents the internal directory where your application stores temporary cache files.

When your device has insufficient storage space, Android may delete these cached files to restore the space. However, you should not rely on the system to clear these files for you. You should always maintain these cached files on your own and keep them within a reasonable space consumption range, such as 1 MB. when you uninstall your application, these files will also be deleted.

Other common methods:

GetFilesDir ()

Obtain the absolute path of your internal files stored in the file system.

GetDir ()

Create (open if any) the directory of your internal bucket.

DeleteFile ()

Delete an object stored in the internal storage.

FileList ()

Returns the files saved by your application as an array of files.

Related Article

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.