How the Android data store is stored

Source: Internet
Author: User

Data can be stored in built-in or removable Storage, database, network, sharedpreference.

Android can use content provider to make your private data leak to other applications.

I. sharedpreference

Two ways to get sharedpreference:

1.getSharedPreferenced (): You can define multiple preference files using this method

2.getPreferenced (): Create only one preference file

Sharedpreference is not strictly used to store "User Configuration", such as storing the user's ringtone. If your application is interested in creating a user configuration, you can use the preferenceactivity,

Two. Built-in storage

Steps to create and write a file:

Use Openfileoutput to get FileOutputStream to write data through this stream.

Read data steps from the built-in storage:

Use Openfileinput to get FileInputStream to read the data through this stream.

If you want to save a static file in your app at compile time, you can save it in the res/raw/path by calling Openrawresource incoming r.raw.<filename> to open.

If you want to cache data instead of persisting the data, you can use Opencachedir () to open an internal storage file when you want to cache the data.

When the device has insufficient built-in storage space, Android will delete the files to restore space. You do not have to delete these files, however, but you should limit the amount of space that the cache file occupies, rather than 1M.

Other useful methods:

Getfiledir (): Get the absolute path to your built-in file.

Getdir () creates (or opens an existing) path.

DeleteFile () Delete a built-in file

FileList () Returns a list of all the built-in files in the current application

Three. External storage

If you want to write external storage and read external storage, just ask WRITE_EXTERNAL_STORAGE for permission.

<manifest ...>    <uses-permission Android:name="Android.permission.WRITE_EXTERNAL_STORAGE"                     android:maxsdkversion=" " />    ...</manifest>
(Add permission when version is less than 18)


Starting with Android4.4, if you want to use the application's private files, you are not allowed to read and write external storage permissions.

You can share this file with other apps by putting the file into music/,pictures/,ringtone/.

If you do not want the system's multimedia browser to be able to browse your multimedia files, you can add. nomedia files to your folder.

methods for getting public folders getextenrnalStoragepublicdirectory () directory_music/directory_pictures/the parameters Directory_ringtone incoming.

to add an album under the Public Pictures folder:

 Public FileGetalbumstoragedir(StringAlbumname) {    //Get The directory for the user's public pictures directory.    Filefile= New File(Environment.getexternalstoragepublicdirectory(            Environment.Directory_pictures),Albumname);    if (!file.mkdirs()) {        Log.e(Log_tag, "Directory not created");    }    returnfile;}
    You can use Getextenrnalfiledir () to get a private multimedia storage path in the external storage

These files will be deleted when the user deletes your app

These files are not discovered by the system's multimedia browser butREAD_EXTERNAL_STORAGE权限的应用是可access的。

the buffer of the external storage space is obtained by Getexternalcachedir (). These files will also be deleted when the application is uninstalled.

need to carefully manage the buffers and delete those that are no longer used in your app's life cycle

Four. Using the database

Five. Using the network

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.