The Android data store's Android 6.0 thinking about file storage under runtime permissions _android

Source: Internet
Author: User
Tags log log

Objective:

File storage is pretty familiar to us in the process of doing app development, but since the Android 6.0 release, access to external sdcard based on runtime permissions is required to dynamically request permissions, So the previous direct SDcard root directory directly created a xxx/cache/directory to do file storage is not so easy to control, so it is necessary to rethink the knowledge of the Android file storage.

Background:

Read and Write permissions for external SDcard

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission Android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>

Before Android 6.0, as long as the above two permissions can be assured bold to read and write on the SDcard, but Android 6.0 after the need to dynamically request read and write permission, so we should fit the Android 6.0来 for file storage. First, we try to do the following when using file storage:

    • Do not arbitrarily occupy the user's built-in storage
    • Do not randomly create a new directory on the SD card, you should put your own application package name corresponding to the extended storage directory, uninstall the app can be automatically cleared.
    • The disk space occupied by the upper limit, and according to a certain policy to clear, such as DISKLRU algorithm.

Android File storage directory:

1.) Apply private storage (built-in storage)

How to access Detailed path Whether to request permission

Context.getfiledir (); Gets the directory of files in the built-in store that can be used to store sensitive data that cannot be exposed to other applications such as user personal information

/data/data/Application Package Name/files/ Whether

Context.getcachedir (); Gets the cache directory under the built-in storage, which can be used to save some cached files such as pictures, and automatically purge the system when the built-in storage is low on space

/data/data/Application Package Name/cache/ Whether

Attention:

Because of the built-in storage of the Android phone, the file browser is inaccessible if the phone does not have root privileges, and the same storage is deleted as the app is deleted.

2.) Application extended Storage (SD card)

How to access Path details Whether to request permission

 Context.getExternalFilesDir();获取SD卡上的文件目录,

sdcard/android/data/Application Package Name/files/

API < 19: Yes

API >= 19: No

Context.getExternalCacheDir();获取SD卡上的缓存目录,可以用来保存一些缓存文件如图片

sdcard/android/data/Application Package Name/cache/

API < 19: Yes

API >= 19: No

Attention:

Because it is stored on the sdcard so as not to have sensitive data such as user information, the files here will be deleted as the app is deleted.

3.) Public Storage (SD card)

How to access Path details Whether to request permission
Environment.getexternalstoragedirectory (); Get sdcard root directory Sdcard/xxx folder name/ Is

Attention:

Sometimes we also need to store some common files, and hopefully they will not be deleted as the app is deleted, such as the video we recorded or the music we downloaded. Since this directory can be accessed by any app, we need to apply for permission when we use it.

Compatible with the Android 6.0 file cache implementation

Premise: Here mainly deal with some non-persistent data, the need to permanently save the data as far as possible or to choose SDcard Public storage mode.

1.) Get Cache root directory

/**
   * Get app root directory
   *
   * @return File cache root Path
   /public
  static String Getdiskcacherootdir () {
    file Diskrootfile;
    if (Existssdcard ()) {
      diskrootfile = Leeapplication.getapp (). Getexternalcachedir ();
    } else {
      Diskrootfile = Leeapplication.getapp (). Getcachedir ();
    String CachePath;
    if (diskrootfile!= null) {
      CachePath = Diskrootfile.getpath ();
    } else {
      throw new IllegalArgumentException ("Disk is invalid");
    }
    return cachepath;
  }
 Here you need to determine whether
 sdcard
   can be used/** * to determine if the external sdcard is normal use
   *
   * @return/public
  static Boolean Existssdcard () {return
    Environment.MEDIA_MOUNTED.equals (environment.getexternalstoragestate ()) | |! Environment.isexternalstorageremovable ();
  }

2.) Get the directory for the specified feature

 /**
   * Get related function Business Directory
   *
   * @return File cache path
  /public static string Getdiskcachedir (String dirname) {
    String dir = String.Format ("%s/%s/", Getdiskcacherootdir (), dirname);
    File File = new file (dir);
    if (!file.exists ()) {
      Boolean issuccess = File.mkdirs ();
      if (issuccess) {
        log.d (TAG, "dir mkdirs success");
      }
    return File.getpath ();
  }

In the daily development process, we need different files in different directories, such as: Log log files need to be placed under the log file, you can pass the above method of "log" to obtain the business function of the folder.

3.) Get the specified feature file path

/**
   * Get log log root
   * @return
   /public
  static String Getlogdir () {return
    getdiskcachedir (log) ;
  }

  /**
   * LogName get log file full path
   * @param logName
   * @return *
   /public
  static String Getlogfilepath ( String logName) {return
    getlogdir () +logname;
  }

Summarize:

Today, we summarize the Android file storage and the 6.0 adaptation issues, recently, I suddenly feel that I have too much knowledge to learn, perhaps because of the current economic environment caused the Internet to encounter the capital of winter, in fact, for me, 2012 has experienced once, for the technical staff should be calm to look at this problem, need to do is is to sow hope in the winter, that is learning. To share the same ~

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.