原文链接:Android存储使用参考

基本操作

权限

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

判断sd卡可用

public static boolean hasSDCardMounted() {    String state = Environment.getExternalStorageState();    if (state != null && state.equals(Environment.MEDIA_MOUNTED)) {        return true;    } else {        return false;    }}

获取可用空间

@TargetApi(VERSION_CODES.GINGERBREAD)public static long getUsableSpace(File path) {    if (path == null) {        return -1;    }    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {        return path.getUsableSpace();    } else {        if (!path.exists()) {            return 0;        } else {            final StatFs stats = new StatFs(path.getPath());            return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();        }    }}

路径图

    ($rootDir)+- /data                  -> Environment.getDataDirectory()|   ||   |   ($appDataDir)|   +- data/com.srain.cube.sample|       ||       |   ($filesDir)|       +- files            -> Context.getFilesDir() / Context.getFileStreamPath("")|       |       ||       |       +- file1    -> Context.getFileStreamPath("file1")|       |   ($cacheDir)|       +- cache            -> Context.getCacheDir()|       ||       +- app_$name        -> Context.getDir(String name, int mode)||   ($rootDir)+- /storage/sdcard0     -> Environment.getExternalStorageDirectory()    |                       / Environment.getExternalStoragePublicDirectory("")    |    +- dir1             -> Environment.getExternalStoragePublicDirectory("dir1")    |    |   ($appDataDir)    +- Andorid/data/com.srain.cube.sample        |        |   ($filesDir)        +- files        -> Context.getExternalFilesDir("")        |   |        |   +- file1    -> Context.getExternalFilesDir("file1")        |   +- Music    -> Context.getExternalFilesDir(Environment.Music);        |   +- Picture  -> ... Environment.Picture        |   +- ...        |        |   ($cacheDir)        +- cache        -> Context.getExternalCacheDir()        |        +- ???

各个路径的特性

1、根目录($rootDir):

内部存储路径: /data, 通过Environment.getDataDirectory() 获取
外部存储路径: /storage/sdcard0 (也有类似 /mnt/ 这样的),通过Environment.getExternalStorageDirectory()获取

Environment.getDataDirectory():         /dataEnvironment.getExternalStorageDirectory():         /storage/sdcard0

2、应用数据目录($appDataDir)

内部储存: appDataDir= rootDir/data/$packageName

外部存储: appDataDir= rootDir/Andorid/data/$packageName

在这些目录下的数据,在app卸载之后,会被系统删除,我们应将应用的数据放于这两个目录中。

3、外部存储中,公开的数据目录。

这些目录将不会随着应用的删除而被系统删除,请斟酌使用。

Environment.getExternalStorageDirectory():     /storage/sdcard0// 同 $rootDirEnvironment.getExternalStoragePublicDirectory(""):     /storage/sdcard0Environment.getExternalStoragePublicDirectory("folder1"):     /storage/sdcard0/folder1

4、应用数据目录下的目录

1)、数据缓存: cacheDir= appDataDir/cache:
内部存储:Context.getCacheDir(),机身内存不足时,文件会被删除
外部存储:Context.getExternalCacheDir(),外部存储没有实时监控,当空间不足时,文件不会实时被删除,可能返回空对象

Context.getCacheDir():         /data/data/com.srain.cube.sample/cacheContext.getExternalCacheDir():       /storage/sdcard0/Android/data/com.srain.cube.sample/cache

2)、文件目录 filesDir= appDataDir/files:
内部存储:通过Context.getFilesDir() 获取
Context.getFileStreamPath(String name)返回以name为文件名的文件对象,name为空,则返回 $filesDir 本身

Context.getFilesDir():         /data/data/com.srain.cube.sample/filesContext.getFileStreamPath(""):        /data/data/com.srain.cube.sample/filesContext.getFileStreamPath("file1"):        /data/data/com.srain.cube.sample/files/file1

外部存储:通过Context.getExternalFilesDir(String type), type为空字符串时获取.

Context.getExternalFilesDir(""):        /storage/sdcard0/Android/data/com.srain.cube.sample/filesContext.getExternalFilesDir(Environment.DIRECTORY_MUSIC)     /storage/sdcard0/Android/data/com.srain.cube.sample/files/Music

3)、 cacheDir/ filesDir 安全性

在内部存储中, cacheDir, filesDir是app安全的,其他应用无法读取本应用的数据,而外部存储则不是。
在外部存储中,这两个文件夹其他应用程序也可访问。

4)、 cacheDir/ filesDir 同级目录

在内部存储中:通过 Context.getDir(String name, int mode)可获取和 filesDir/ cacheDir 同级的目录

目录的命名规则为 app_ + name, 通过mode可控制此目录为app私有还是其他app可读写。

Context.getDir("dir1", MODE_PRIVATE):        Context.getDir: /data/data/com.srain.cube.sample/app_dir1

5)、特别注意, 对于外部存储,获取 cacheDir filesDir及其下的路径
在API level 8 以下,或者空间不足,相关的方法获路径为空时,需要自己构造。

@TargetApi(VERSION_CODES.FROYO)public static File getExternalCacheDir(Context context) {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)) {        File path = context.getExternalCacheDir();        // In some case, even the sd card is mounted,        // getExternalCacheDir will return null        // may be it is nearly full.        if (path != null) {            return path;        }    }    // Before Froyo or the path is null,    // we need to construct the external cache folder ourselves    final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";    return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);}

更多相关文章

  1. Android中的存储路径之外部存储
  2. Android数据库操作的两种方式
  3. Android studio 连接数据库小经历遇到的问题以及解决方法(java.sq
  4. Android数据的四种存储方式SharedPreferences、SQLite、Content
  5. android中SharedPreferences和PreferenceActivity的存取数据
  6. android复制数据库到SD卡(网上搜集,未经验证)
  7. Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库
  8. 【Android的从零单排开发日记】之入门篇(三)——Android目录结构
  9. android developers api guides 文档学习笔记(目录)

随机推荐

  1. Mysql InnoDB和MyISAM区别原理解析
  2. Mysql 聚合函数嵌套使用操作
  3. mysql查询的时候给字段赋默认值操作
  4. mysql 主从复制如何跳过报错
  5. MySql分组后随机获取每组一条数据的操作
  6. 获取 MySQL innodb B+tree 的高度的方法
  7. MySQL之范式的使用详解
  8. linux使用mysqldump+expect+crontab实现m
  9. SQL 列不同的表查询结果合并操作
  10. MySQL5.7.31 64位免安装版使用教程图解