Android File Basics

Internal Storage and External Storage

insert image description here

Before Android4.4, internal storage (Internal Storage) refers to the storage of the phone body, while external storage (External Storage) refers to the external SD card.

In Android 4.4 and later, if there is no external SD card, the body storage is divided into internal storage (Internal Storage) and external storage (External Storage); if an external SD card is added, the external SD card and the machine The external storage of the personal storage is counted as external storage (External Storage)

//在Android4.4及以后提供了getExternalFilesDirs方法来获取所有的外置存储
File[] files =  getExternalFilesDirs("");

Folders in the Android system

insert image description here

data directory

/dataThe directory is the so-called internal storage (ROM), but this folder cannot be opened when the phone is not rooted.

  1. data/app/

Store all apk files installed by users, excluding system default application files

  1. data/data/package name/

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-jv9LwiLm-1661177297780)(https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx /gold-user-assets/2020/3/29/171257f84ba6265b~tplv-t2oaga2asx-image.image)]

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

  • cache: cache directory
  • databases: database directory
  • files: file storage directory
  • shared_prefs: SharedPreferences file storage directory
  1. /data/misc/

Most of the WIFI and VPN information are stored here.

  1. /data/user/0/

/data/dataThe directory is actually a soft link to /data/user/0the directory, which /data/datais equivalent to a pointer to
/data/user/0the directory.

After Android 4.2, Android launched a "multi-user" feature, which means that a device may be used by multiple people at the same time, so it is necessary to separate each person's data, applications, and personality configuration. In order to distinguish data, numbers are used to distinguish multiple users, where 0 refers to the first user

Soft links are available to see Linux soft links and hard links

system

Android system files, root permissions are required

  1. /system/app/

Some system apps are stored here

  1. /system/fonts/ directory

The font files of the system are stored here

  1. /system/lib/ directory

Almost all shared library (.so) files are stored here.

  1. /system/media/ directory

This is used to save system ringtones and system prompts.

storage

storageThe directory is the external storage, including the internal storage of the fuselage and the external SD card. Note that different versions store data in different locations, for example:

Androidr4.4.2 with SD card (the phone without Android4.4, the data comes from the network)

Location path api to get path
external storage /storage/emulated/0/Android/data/packname/files getExternalStorageDirectory()
External SD card /storage/sdcard1 getExternalFilesDirs()

Android 8 Xiaomi

Location path api to get path
external storage /storage/emulated/0/Android/data/packname/files getExternalStorageDirectory()
External SD card /storage/sd_name getExternalFilesDirs()

sdcard

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

Get directory in Andorid

The values ​​returned by the following APIs are the values ​​returned under Android O

internal storage directory

    //创建/data/user/0/packagename/files文件夹,随着应用位置的改变,位置可能改变
      context.getFilesDir();
       //创建/data/user/0/packagename/cache文件夹当需要空间时,
       //系统会自动删除其中的文件(先删除存在最长时间的)
      context.getCacheDir();
      // 获取 /data 目录
      Environment.getDataDirectory()

Note: Reading and writing the file object returned by the above method does not require permission. When the user uninstalls the app, the system will delete all the files related to the app in the data/data directory.

Private directory for external storage

     //一般存放临时缓存数据,对应 清除缓存
     //目录为 /storage/emulated/0/Android/data/packagename/cache
      context.getExternalCacheDir()/**
       *一般放一些长时间保存的数据,对应 清除数据
       *type的取值
       *null: /storage/emulated/0/Android/data/com.example.androidbase/files
       *DIRECTORY_MUSIC *Environment.DIRECTORY_MUSIC时:/storage/emulated/0/Android/data/com.example.androidbase/file*s/Music  
       * DIRECTORY_PODCASTS
       * DIRECTORY_RINGTONES
       * DIRECTORY_ALARMS
       * DIRECTORY_NOTIFICATIONS
       * DIRECTORY_PICTURES
       * DIRECTORY_MOVIES
       * 自定义字符串:/storage/emulated/0/Android/data/com.example.androidbase/files/自定义字符串
       */
      context.getExternalFilesDir(String type)//使用Environment来获取,需要获取权限来操作
      Environment.getExternalStorageDirectory();

Methods 1 and 2 do not require permission, while method 3 needs to apply for permission. The characteristics of this directory are:

  • The system generally does not clean up the files in it
  • Since ordinary users may freely operate the files in it, a non-null judgment and exception capture should be added when using it
  • Other applications obtained WRITE_EXTERNAL_STORAGEcan operate the file, and after Android 7.0, it must be accessed through FileProvider
  • Starting from Android 4.4, the app can directly read and write the application private directory in the external storage space, so that developers do not need to apply for file read and write permissions in the manifest file or dynamically in the external storage space.
  • Deleted after app uninstall

Public directory for external storage

        /**
         * 需要申请权限
         * Type的值看上面(不能为null),注意要创建文件时不能使用 File的 mkdir() 和 mkdirs() 方法,它们只会创建目录,可以使用
         *createNewFile()或者不使用 都可以创建文件
         *空字符串时:/storage/emulated/0
         *自定义字符串:/storage/emulated/0/自定义字符串
         */
        Environment.getExternalStoragePublicDirectory(String type);

Get the total size and available size of the outer space and inner space

Look at Android's acquisition of storage mobile phone storage capacity

Android multimedia data information

Reprinted from Android Multimedia: MediaProvider, MediaStore

MediaProvider is a database in the Android system, also known as a multimedia database, which saves the information of all files stored on the mobile phone. This database is stored in /data/data/com.android.providers.media/databases, which contains two databases: internal.db and external.db, internal.db stores the file information of the system partition, the developer does not It is not possible to obtain the information through the interface, while external.db stores the file information in the storage area that our users can see, which includes the built-in storage of the mobile phone and the SD card.

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

MediaStore inner class

  • class MediaStore.Audio: container for all audio content
  • class MediaStore.Files: The MediaProvider table contains an index of all files in the media store, including non-media files
  • class MediaStore.Images: container for all image content
  • interface MediaStore.MediaColumns: public fields of the MediaProvider table
  • class MediaStore.Video: container for all video content

MediaStore.MediaColumns field description

  • String DATA: Path to the file on disk
  • String DATE_ADDED: The time when the file was added to the media provider (in seconds)
  • String DATE_MODIFIED: The time the file last modified the unit
  • String DISPLAY_NAME: The display name of the file
  • String HEIGHT: the height of the image/video in pixels
  • String MIME_TYPE: MIME type of the file
  • String SIZE: file size in bytes
  • String TITLE: title
  • String WIDTH: The width of the image/video in pixels.

combat

Imitation QQ local picture selector

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();

The effect is as shown in the figure

Please add a picture description

reference

  • https://blog.csdn.net/csdn_aiyang/article/details/80665185
  • https://blog.csdn.net/dongyaqin/java/article/details/88947467
  • https://blog.csdn.net/qq_39074954/java/article/details/97613088

Guess you like

Origin blog.csdn.net/lichukuan/article/details/126474636