Internal storage and external storage

Before Android4.4, Internal Storage was the phone’s body Storage, while External Storage was the External SD card.

In Android4.4 and later, in the absence of External SD card in the case of the body Storage is divided into Internal Storage and External Storage; If an External SD card is added, the External SD card and the External Storage of the body are counted as External Storage.

// the getExternalFilesDirs method is provided in Android4.4 and later to retrieve all external storage
File[] files =  getExternalFilesDirs("");
Copy the code

Folder in Android system

The data directory

The /data directory is called internal storage (ROM), but cannot be opened when the phone does not have root.

  1. data/app/

Store all apK files installed by users, excluding default application files

  1. The data/data/package name

The data for each application is stored in a folder named after the package name, as shown below:

  • Cache: indicates the cache directory
  • Databases: indicates the database directory
  • Files: indicates the file storage directory
  • Shared_prefs :SharedPreferences file storage directory
  1. /data/misc/

This is where most WIFI and VPN information is stored.

  1. /data/user/0/

The /data/data directory is actually soft-wired to the /data/user/0 directory, equivalent to the /data/data directory is a pointer to the /data/user/0 directory.

After Android4.2, Android introduced a “multi-user” feature, meaning that a device could be used by more than one person at a time, so it needed to separate each person’s data, applications, and personality profile. In order to distinguish data, we use numbers to distinguish multiple users, where 0 is the first user

Soft connections see Linux soft connections and hard links

system

Android system file, requiring root permission

  1. /system/app/

Here are some apps for the system

  1. / system/fonts/directory

This is where the font files of the system are stored

  1. / system/lib/directory

This is where almost all the shared library (.so) files are stored.

  1. / system/media/directory

Save system ringtones and system announcements.

storage

The storage directory is the external storage, including internal storage and external SD card. Note Different versions store data in different locations. For example:

Androidr4.4.2 with SD card (phones without Android4.4, data from the web)

location The path Get the API of the path
Fuselage external storage /storage/emulated/0/Android/data/packname/files getExternalStorageDirectory()
External SD card /storage/sdcard1 getExternalFilesDirs()

Android 8 millet

location The path Get the API of the path
Fuselage external storage /storage/emulated/0/Android/data/packname/files getExternalStorageDirectory()
External SD card /storage/sd_name getExternalFilesDirs()

sdcard

For details, see the analysis of confusable points of Android internal and external storage (/ MNT /sdcard, /storage/sdcard0, /storage/emulated/0, etc.)

Gets the directory in Andorid

The values returned by the following apis are those returned by Android O

Internal storage directory

    / / create/data/user / 0 / packagename/files folder, with the change of the application of location, location may change
      context.getFilesDir();
       / / create/data/user / 0 / packagename/cache folder when need space,
       // The system will automatically delete the files in it (delete the files that exist longest first)
      context.getCacheDir();
      // Get the /data directory
      Environment.getDataDirectory()
Copy the code

Note: There is no need to apply permission for the file object returned by reading and writing the above method. When the user uninstalls the APP, the system will delete all files related to the APP in the data/data directory

Private directory for external storage

     // Generally store temporary cache data, corresponding to clear cache
     / / for/with/storage/emulated / 0 / Android/data/packagename/cacheThe context. GetExternalCacheDir () :/** * Delete data *type *null: / storage/emulated / 0 / Android/data/com. Example. Androidbase DIRECTORY_MUSIC/files * * Environment. DIRECTORY_MUSIC when: /storage/emulated/0/Android/data/com.example.androidbase/file*s/Music * DIRECTORY_PODCASTS * DIRECTORY_RINGTONES * DIRECTORY_ALARMS * DIRECTORY_NOTIFICATIONS * DIRECTORY_PICTURES * DIRECTORY_MOVIES * Custom string: / storage/emulated / 0 / Android/data/com. Example. Androidbase/files / * / custom stringThe context. GetExternalFilesDir (String type) :// Use Environment to obtain the permission to operate
      Environment.getExternalStorageDirectory();
Copy the code

Methods 1 and 2 do not require permission, while method 3 requires permission application. This directory has the following characteristics:

  • The system generally does not clean up files inside
  • Since ordinary users may freely manipulate the files inside, they should add a non-null judgment and exception catching
  • To obtainWRITE_EXTERNAL_STORAGEThis file can be accessed by other applications in Android 7.0 or later via the FileProvider
  • Since Android 4.4, apps can directly read and write application private directories in the external storage space. Developers do not need to apply for read and write permissions in the Manifest file or dynamically.
  • Deleted after the application is uninstalled

Public directory for external storage

        /** * need to request permission * Type value (cannot be null), note that to create a File, do not use the File mkdir() and mkdirs() methods, they only create directories, *createNewFile() can be used to create a file * Empty string: /storage/emulated/0 * Custom string: /storage/emulated/0/ custom string */
        Environment.getExternalStoragePublicDirectory(String type);
Copy the code

Gets the total size and available size of external and internal space

See Android to obtain storage phone storage capacity

Android multimedia data information

Reprinted from Android Multimedia: MediaProvider, MediaStore

MediaProvider is a database in the Android system, also known as multimedia database, which stores information about all files stored on the mobile phone. The database is stored in/data/data/com. Android will. The media/databases, there are two databases: Internal. Db and external.db, internal. Db stores the file information of system partition, developers can not get the information through the interface, while external. An SD card is also included.

MediaStore provides metadata about all the media available on internal and external storage devices.

MediaStore inner class

  • Class Mediastore. Audio: A container for all Audio content
  • Class MediaStore. Files: MediaProvider tables containing all the Files of the media storage indexes, including the media Files
  • Class Mediastore. Images: Container for all image content
  • Interface MediaStore. MediaColumns: MediaProvider table public fields
  • Class Mediastore. Video: Container for all Video content

MediaStore.MediaColumns Field description

  • String DATA: indicates the path of the file on the disk
  • String DATE_ADDED: Time at which files are added to the Media provider (in seconds)
  • String DATE_MODIFIED: time when the file was last modified
  • String DISPLAY_NAME: display name of the file
  • String HEIGHT: HEIGHT of the image/video, in pixels
  • String MIME_TYPE: MIME type of the file
  • String SIZE: indicates the byte SIZE of the file
  • String TITLE: TITLE
  • String WIDTH: The WIDTH of the image/video, in pixels.

In actual combat

Imitation QQ local picture picker

String[] columns = {MediaStore.Images.Media._ID, MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Media.DATA, MediaStore.Images.Media.BUCKET_ID,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,MediaStore.MediaColumns.MIME_TYPE,MediaStore.MediaColumns.SIZE
};
String sortOrder = MediaStore.Images.Media.DATE_ADDED+" desc";
Cursor cursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null.null, sortOrder);
LinkedHashMap<String,List<ImageBean>> map = new LinkedHashMap<>();
while (cursor.moveToNext()){
    String id = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID));
    String thumbnailsPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
    String path =  cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    String groupId = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID));
    String groupName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
    String type = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.MIME_TYPE));
    String size = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.SIZE));
    ImageBean imageBean = new
            ImageBean(id,thumbnailsPath,path,groupId,groupName,type,size);
    if (map.containsKey(groupId)){
        map.get(groupId).add(imageBean);
    }else {
        ImageGroupBean imageGroupBean = new ImageGroupBean(groupId,groupName,path);
        group.add(imageGroupBean);
        List<ImageBean> l = new ArrayList<>();
        l.add(imageBean);
        map.put(groupId,l);
    }
}
cursor.close();
Copy the code

The effect is shown in figure

reference

  • Blog.csdn.net/csdn_aiyang…
  • Blog.csdn.net/dongyaqin/j…
  • Blog.csdn.net/qq_39074954…