Storage of android data

Source: Internet
Author: User

Data can be stored in Built-in or removable storage, databases, networks, and sharedpreference.

Android can use Content provider to expose your private data to other applications.

I. sharedpreference

Two methods for obtaining sharedpreference:

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

2. getPreferenced (): creates only one preference file.

Sharedpreference is not strictly used to store "User configurations", such as storing user ringtones. If your application is interested in creating user configurations, you can use PreferenceActivity,

2. built-in storage

To create and write a file:

Use openFileOutPut to get FileOutputStream, which writes data.

Steps for reading data from built-in storage:

Use openFileInput to obtain FileInputStream, which reads data.

If you want to save a static file in your application during compilation, you can save it in res/raw/path and input R. raw by calling openRawResource. Open.

If you want to cache data instead of making the data persistent, you can use openCacheDir () to open a file in an internal storage space.

When the built-in storage space of the device is insufficient, Android will delete these files to restore the space. However, you do not have to delete these files, but you should limit the size of the cached files.

Other useful methods:

GetFileDir (): Get the absolute path of your built-in file.

GetDir () creates (OR opens an existing) path.

DeleteFile () deletes a built-in File

FileList () returns the list of all built-in files in the current application.

Iii. External Storage

If you want to write external storage and read external storage, you only need to requestWRITE_EXTERNAL_STORAGE.

     
      ...
 
(When the version is earlier than 18, add permissions)


From Android4.4 to use private files of an application, you are not allowed to read or write external storage.

You can share the file with other applications by placing the file in Music/, Pictures/, and RingTone.

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

Get the public folder method getExtenrnalStoragePublicDirectory () and pass in the DIRECTORY_MUSIC/DIRECTORY_PICTURES/DIRECTORY_RINGTONE parameter.

Add an album in the public image folder as follows:

public File getAlbumStorageDir(String albumName) {    // Get the directory for the user's public pictures directory.    File file = new File(Environment.getExternalStoragePublicDirectory(            Environment.DIRECTORY_PICTURES), albumName);    if (!file.mkdirs()) {        Log.e(LOG_TAG, "Directory not created");    }    return file;}
You can use getExtenrnalFileDir () to obtain the private Multimedia Storage path in external storage.

When a user deletes your application, these files will be deleted.

These files will not be discovered by the system's multimedia browser,Applications with the READ_EXTERNAL_STORAGE permission are accessible.

GetExternalCacheDir () is used to obtain the cache area of the external storage space. These files will also be deleted when the application is uninstalled.

You need to carefully manage the cache area and delete the caches that are no longer used during your app lifecycle.

4. Use the database

5. Use networks

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.