context获取路径
context.getFilesDir()
获取路径:/data/user/0/应用包名/files
包含应用程序文件的目录的路径。不需要额外的权限来读取或在返回的路径下写入文件。1
2
3
4
5
6
7/**
* No additional permissions are required for the calling app to read or
* write files under the returned path.
*
* @return The path of the directory holding application files.
*/
public abstract File getFilesDir();
context.getCacheDir()
获取路径:/data/user/0/应用包名/cache
应用程序缓存文件的目录的路径。强烈鼓励应用程序将缓存空间的使用保持在满额。当该文件夹超额时,系统将自动删除该目录中的文件
为其他地方提供需要空间,当不满额时则不会。不需要额外的权限来读取或在返回的路径下写入文件。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19/**
* The system will automatically delete files in this directory as disk
* space is needed elsewhere on the device. The system will always delete
* older files first, as reported by {@link File#lastModified()}.
* <p>
* Apps are strongly encouraged to keep their usage of cache space below the
* quota returned by
* {@link StorageManager#getCacheQuotaBytes(java.util.UUID)}. If your app
* goes above this quota, your cached files will be some of the first to be
* deleted when additional disk space is needed. Conversely, if your app
* stays under this quota, your cached files will be some of the last to be
* deleted when additional disk space is needed.
* <p>
* Apps require no extra permissions to read or write to the returned path,
* since this path lives in their private storage.
*
* @return The path of the directory holding application cache files.
*/
public abstract File getCacheDir();
context.getExternalCacheDir()
获取路径:/storage/emulated/0/Android/data/应用包名/cache
应用程序特定目录的绝对路径,与 mContext.getCacheDir() 相似,区别在于平台并不总是监视共享存储中可用的空间,
因此可能不会自动删除这些文件。这些文件是应用程序内部的,通常不作为媒体文件对用户展示。1
2
3
4
5
6
7
8
9
10
11
12
13
14/**
* These files are internal to the application, and not typically visible
* to the user as media.
* <p>
* This is like {@link #getCacheDir()} in that these files will be deleted
* when the application is uninstalled, however there are some important
* differences:
* <li>The platform does not always monitor the space available in shared
* storage, and thus may not automatically delete these files. Apps should
* always manage the maximum space used in this location.
*
* @return the absolute path to application-specific directory. May return
* {@code null} if shared storage is not currently available.
*/
context.getExternalFilesDir(String type)
获取路径:/storage/emulated/0/Android/data/应用包名/files/type指定目录(详见下面Environment分析)
应用程序特定目录的绝对路径,与 mContext.getFilesDir() 相似。这些文件是应用程序内部的,通常不作为媒体文件对用户展示。1
2
3
4
5
6
7
8
9
10
11
12
13
14/**
* @param type The type of files directory to return. May be {@code null}
* for the root of the files directory or one of the following
* constants for a subdirectory:
* {@link android.os.Environment#DIRECTORY_MUSIC},
* {@link android.os.Environment#DIRECTORY_PODCASTS},
* {@link android.os.Environment#DIRECTORY_RINGTONES},
* {@link android.os.Environment#DIRECTORY_ALARMS},
* {@link android.os.Environment#DIRECTORY_NOTIFICATIONS},
* {@link android.os.Environment#DIRECTORY_PICTURES}, or
* {@link android.os.Environment#DIRECTORY_MOVIES}.
* @return the absolute path to application-specific directory. May return
* {@code null} if shared storage is not currently available.
*/
context.getObbDir()
获取路径:/storage/emulated/0/Android/obb/应用包名
返回到应用程序特定目录的绝对路径。可能返回NULL。不需要额外权限。1
2
3
4
5
6
7
8
9
10
11
12
13
14/**
* Note if the application does not have any OBB files, this directory may not exist.
* <p>
* This is like {@link #getFilesDir()} in that these files will be deleted
* when the application is uninstalled.
*
* On devices with multiple users (as described by {@link UserManager}),
* multiple users may share the same OBB storage location. Applications
* should ensure that multiple instances running under different users don't
* interfere with each other.
*
* @return the absolute path to application-specific directory. May return
* {@code null} if shared storage is not currently available.
*/
context.getNoBackupFilesDir()
获取路径:/data/user/0/应用包名/no_backup
不会自动备份到远程存储的应用程序文件的路径。需要判断Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP(21)。1
2
3
4
5
6
7
8
9
10/**
* Returns the absolute path to the directory on the filesystem similar to
* {@link #getFilesDir()}. The difference is that files placed under this
* directory will be excluded from automatic backup to remote storage. See
* {@link android.app.backup.BackupAgent BackupAgent} for a full discussion
* of the automatic backup mechanism in Android.
*
* @return The path of the directory holding application files that will not
* be automatically backed up to remote storage.
*/
context.getCodeCacheDir()
获取路径:/data/user/0/应用包名/code_cache
保存应用程序代码缓存文件的目录路径。适合在运行时存放应用产生的编译或者优化的代码。不需要额外权限。需要判断Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP(21)。1
2
3
4
5
6
7
8
9
10/**
* The system will delete any files stored in this location both when your
* specific application is upgraded, and when the entire platform is
* upgraded.
* <p>
* This location is optimal for storing compiled or optimized code generated
* by your application at runtime.
*
* @return The path of the directory holding application code cache files.
*/
context.getDataDir()
获取路径:/data/user/0/应用包名
返回文件系统目录的绝对路径,所有属于该应用程序的私有文件都存储在文件系统中。应用程序不应该直接使用这个路径,
而是应该在这个类上使用{getFilesDir()}、{getCacheDir()}、{getDir(String, int)}或其他存储API。
需要判断Build.VERSION.SDK_INT >= Build.VERSION_CODES.N(24)。1
2
3
4
5
6
7
8
9
10/**
* Returns the absolute path to the directory on the filesystem where all
* private files belonging to this app are stored. Apps should not use this
* path directly; they should instead use {@link #getFilesDir()},
* {@link #getCacheDir()}, {@link #getDir(String, int)}, or other storage
* APIs on this class.
*
* No additional permissions are required for the calling app to read or
* write files under the returned path.
*/
context.getPackageCodePath()
获取路径:/data/app/应用包名-xxxxx/base.apk
此上下文的主Android包的完整路径。这对应用程序通常没有用处,因为它们不应该直接访问文件系统。1
2
3
4
5
6
7
8
9
10/**
* Return the full path to this context's primary Android package.
* The Android package is a ZIP file which contains application's
* primary code and assets.
*
* <p>Note: this is not generally useful for applications, since they should
* not be directly accessing the file system.
*
* @return String Path to the code and assets.
*/
context.getPackageResourcePath()
获取路径:/data/app/应用包名-xxxxx/base.apk
基本跟mContext.getPackageCodePath()相同。
Environment
存储状态
存储状态 | 描述 |
---|---|
MEDIA_UNKNOWN | Unknown storage state, such as when a path isn’t backed by known storage media. |
MEDIA_REMOVED | Storage state if the media is not present. |
MEDIA_UNMOUNTED | Storage state if the media is present but not mounted. |
MEDIA_CHECKING | Storage state if the media is present and being disk-checked. |
MEDIA_NOFS | Storage state if the media is present but is blank or is using an unsupported filesystem. |
MEDIA_MOUNTED | Storage state if the media is present and mounted at its mount point with read/write access. |
MEDIA_MOUNTED_READ_ONLY | Storage state if the media is present and mounted at its mount point with read-only access. |
MEDIA_SHARED | Storage state if the media is present not mounted, and shared via USB mass storage. |
MEDIA_BAD_REMOVAL | Storage state if the media was removed before it was unmounted. |
MEDIA_UNMOUNTABLE | Storage state if the media is present but cannot be mounted. Typically this happens if the file system on the media is corrupted. |
MEDIA_EJECTING | Storage state if the media is in the process of being ejected. |
标准存储目录
标准存储目录 | 目录 |
---|---|
DIRECTORY_MUSIC | Music 目录中的任何音频文件应该放在常规的音乐列表中供用户使用。 |
DIRECTORY_PODCASTS | Podcasts 可放置任何音频文件,应该在用户可以选择的播客列表中(而不是常规音乐)。 |
DIRECTORY_RINGTONES | Ringtones 可放置任何音频文件,应该在用户可以选择的铃声列表中(而不是常规音乐)。 |
DIRECTORY_ALARMS | Alarms 可放置任何音频文件,应该在用户可以选择的闹铃列表中(而不是常规音乐)。 |
DIRECTORY_NOTIFICATIONS | Notifications 可放置任何音频文件,应该在用户可以选择的通知提醒列表中(而不是常规音乐)。 |
DIRECTORY_PICTURES | Pictures 其中放置可供用户使用的图片文件。 |
DIRECTORY_MOVIES | Movies 其中放置可供用户使用的视频文件。 |
DIRECTORY_DOWNLOADS | Download 其中放置用户的下载文件 |
DIRECTORY_DCIM | DCIM 传统的安装图片和视频的位置。 |
DIRECTORY_DOCUMENTS | Documents 其中放置由用户创建的文档。 |
context.getExternalFilesDir(String type) 会返回/storage/emulated/0/Android/data/应用包名/files/type指定目录
Environment.getExternalStoragePublicDirectory(String type) 会返回/storage/emulated/0/type指定目录
获取路径
- Environment.getExternalStorageState() 获取存储状态
- Environment.getDataDirectory()
- Environment.getDownloadCacheDirectory()
- Environment.getRootDirectory()
- Environment.getExternalStorageDirectory():
返回共享/外部存储主目录。如果用户已经挂载在他们的计算机上或者已经从设备上删除或者其他一些问题已经发生,这个目录现在可能是不可访问的。可以用Environment.getExternalStorageState() 确定其当前状态。
传统上,这是一个SD卡,但它也可以被实现为与被保护的内部存储区不同的设备中的内置存储器,并且可以作为计算机上的文件系统安装。
应用程序不应直接使用此顶级目录,以避免污染用户的根命名空间。可以通过Context.getExternalFilesDirs(String)、Context.getExternalCacheDirs() 等方法访问二级存储。任何应用程序私有的文件都应该放在Context.getExternalFilesDir 返回的目录中,应用程序被卸载时系统将负责删除。