How to get the use of extended storage paths correctly when Android is developed

Source: Internet
Author: User

How to get the use of extended storage paths correctly when Android is developed Let's introduce Android storage

In the 2.x version, Android devices are single storage, third-party apps write files, must apply for write_external_storage permissions;

After 4.0, Android devices began to have built-in flash memory, primary storage, and an external SD card, secondary external storage device;

Write_external_storage permissions become just control primary STORAGE, and Write_media_storage permissions are introduced to control the operation of secondary EXTERNAL STORAGE device.

To Android 4.4 Kitkat,write_media_storage permissions are only available to system apps and no longer granted to third party apps.

There are also new rules for writing secondary external storage device.

The documentation for Android is written like this:

Link:http://source.android.com/devices/tech/storage/index.html:

The Write_external_storage permission must only grant WRITE access to
The primary external storage on a device. Apps must not being allowed to
Write to secondary external storage devices, except in their
Package-specific directories as allowed by synthesized permissions.
Restricting writes in this-ensures the system can clean up files
When applications is uninstalled.

Translation:
Write_external_storage permissions, only for authorized users to write primary EXTERNAL STORAGE, except for the folder associated with their own package name, the application is not allowed to write secondary EXTERNAL STORAGE Devices

For example, if the package name of the app is Com.example.foo, then the android/data/com.example.foo/folder on the external storage can be accessed arbitrarily, no other place is allowed to write, and the file stored in the folder associated with its own package name, It is also cleared when the app is uninstalled.

In terms of the situation:

  • Only externally stored devices
    This kind of equipment is usually android4.0 before, only a storage, not by this rule limit, or can be casually read and write, but if you brush 4.4 system, then you can only write their own package name related folders.

  • Only devices that are internally stored
    For example, the Nexus series, the Sony L series, is not limited by this rule, but it is recommended to write the data in the folder of their own package name.

  • Both internal storage and external storage
    Need to comply with this rule, can not be in the external storage of the write-down, need to be in their own package name related folder write data.

Google has solved this problem by making this restriction:

Any app, will build a directory on/sdcard,/sdcard1, delete will also be re-built, even if uninstalled, will leave some junk files.

However, a problem has also been created:

Similar to video, image processing this want to cache a large number of audio and video files in the external storage, and the app is uninstalled and want to keep, there is no way.

How should I use it in development?

As a programmer, presumably you also hate the app in the SD card root directory of the mess, then from my start, to abide by the Google rules.

Through the Context.getexternalfilesdir () method can obtain to sdcard/android/data/{package_name}/files/, stores some long time to save the data;

Through the Context.getexternalcachedir () method can obtain to sdcard/android/data/{package_name}/cache/, stores the temporary cache data;

These two directories correspond to the "clear data" and "clear cache" options in the app-to-application details, settings, respectively.

An example of obtaining an external storage cache:

/**

* Get the absolute path to expand storage Cache  *  * @param context *  /public static String Getexternalcachedir (context context) {      if (!ismounted ())           return null;      StringBuilder sb = new StringBuilder ();      File File = Context.getexternalcachedir ();      In some case, even the SD card is mounted,      //Getexternalcachedir'll return NULL      //May be it's nearly full .      if (file = null) {           sb.append (File.getabsolutepath ()). append (File.separator);      } else {           sb.append ( Environment.getexternalstoragedirectory (). GetPath ()). Append ("/android/data/"). Append (Context.getpackagename ())                     . Append ("/cache/"). Append (File.separator). toString ();      return sb.tostring (); }

Reference:
Https://plus.google.com/+TodLiebeck/posts/gjnmuaDM8sn
http://blog.csdn.net/olevin/article/details/29575127
Http://source.android.com/devices/tech/storage/index.html

How to get the use of extended storage paths correctly when Android is developed

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.