Android notes: Storage related, Getexternalcachedir, getexternalfilesdir,getexternalstoragedirectory, etc.

Source: Internet
Author: User


File Cachedir = Mcontext.getexternalcachedir (); if (null! = Cachedir) {Mcachedirpath = Cachedir.getabsolutepath () + "/imag Es/";} if (Textutils.isempty (Mcachedirpath)) {if (Environment.MEDIA_MOUNTED.equals (Environment.getexternalstoragestate () ) {Mcachedirpath = Environment.getexternalstoragedirectory (). GetPath () + "/android/data/com.meiyaapp.meiya/cache/   Images/"; }else{Toast.maketext (Mcontext, "SD card status error, please adjust and try again.")   ", Toast.length_short). Show (); }}














Reference article: http://my.oschina.net/liucundong/blog/314520


Android app correct use of Extended SD card first 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 Primary external storage on a device. Apps must not being allowed TO 
write to secondary external storage devices, except I n their 
package-specific directories as allowed by synthesized permissions.& nbsp
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.

    • only externally stored devices  

    • only internally stored devices  
      such as the Nexus Series, the Sony L series, are not limited by this rule, but it is recommended to write data in the folder of their own package name.

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 will return null       // may be it is 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













This article from the "Sky no traces but I flew" blog, please be sure to keep this source http://glblong.blog.51cto.com/3058613/1699245

Android notes: Storage-related, Getexternalcachedir, getexternalfilesdir,getexternalstoragedirectory, etc.

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.