获取应用专属缓存目录

获取Android应用专属缓存存储目录

SD卡缓存目录

当应用需要将图片或者文件缓存到SD卡中时要去申请创建目录,有下面几种途径 
我们可以通过API调用应用专属目录:

// /storage/emulated/0/Android/data/app_package_name/files/Pictures
Content.getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
// /storage/emulated/0/Android/data/app_package_name/cache
Content.getExternalCacheDir(); 
  
  
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

上面两个目录是专属于当前app的,当应用被删除时,上面目录下的文件也会清空

内存缓存目录

相对于应用的专属SD卡缓存有两个内存缓存地址:

Content. getCacheDir(); //  /data/data/app_package_name/cache
Content. getFilesDir(); //  /data/data/app_package_name/files
  
  
  • 1
  • 2
  • 1
  • 2

这两个目录中的文件也会随着app的删除而清空

当系统版本大于等于4.4时,对通过上面4个API调用得到的目录进行文件的读写操作不需要申请SD卡的读写权限,所以6.0及以上系统使用时也不需要动态申请读写权限

使用注意事项

  1. 当存储比较大的文件时,如图片等文件存储在SD卡对应的目录下
  2. 应用的内存缓存目录只有应用本身能对其进行读写操作,外部应用不行,如相机应用 (内存目录读写权限:rwxr-x–x,SD卡缓存目录读写权限:rwxrwx—)
  3. 即使是通过自定义路径得到的上述目录,在系统版本大于等于4.4时也不需要申请SD卡读写权限

/**
 * 获取应用专属缓存目录
 * android 4.4及以上系统不需要申请SD卡读写权限
 * 因此也不用考虑6.0系统动态申请SD卡读写权限问题,切随应用被卸载后自动清空 不会污染用户存储空间
 * @param context 上下文
 * @param type 文件夹类型 可以为空,为空则返回API得到的一级目录
 * @return 缓存文件夹 如果没有SD卡或SD卡有问题则返回内存缓存目录,否则优先返回SD卡缓存目录
 */
public static File getCacheDirectory(Context context,String type) {
    File appCacheDir = getExternalCacheDirectory(context,type);
    if (appCacheDir == null){
        appCacheDir = getInternalCacheDirectory(context,type);
    }

    if (appCacheDir == null){
        Log.e("getCacheDirectory","getCacheDirectory fail ,the reason is mobile phone unknown exception !");
    }else {
        if (!appCacheDir.exists()&&!appCacheDir.mkdirs()){
            Log.e("getCacheDirectory","getCacheDirectory fail ,the reason is make directory fail !");
        }
    }
    return appCacheDir;
}

/**
 * 获取SD卡缓存目录
 * @param context 上下文
 * @param type 文件夹类型 如果为空则返回 /storage/emulated/0/Android/data/app_package_name/cache
 *             否则返回对应类型的文件夹如Environment.DIRECTORY_PICTURES 对应的文件夹为 .../data/app_package_name/files/Pictures
 * {@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}.or 自定义文件夹名称
 * @return 缓存目录文件夹 或 null(无SD卡或SD卡挂载失败)
 */
public static File getExternalCacheDirectory(Context context,String type) {
    File appCacheDir = null;
    if( Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        if (TextUtils.isEmpty(type)){
            appCacheDir = context.getExternalCacheDir();
        }else {
            appCacheDir = context.getExternalFilesDir(type);
        }

        if (appCacheDir == null){// 有些手机需要通过自定义目录
            appCacheDir = new File(Environment.getExternalStorageDirectory(),"Android/data/"+context.getPackageName()+"/cache/"+type);
        }

        if (appCacheDir == null){
            Log.e("getExternalDirectory","getExternalDirectory fail ,the reason is sdCard unknown exception !");
        }else {
            if (!appCacheDir.exists()&&!appCacheDir.mkdirs()){
                Log.e("getExternalDirectory","getExternalDirectory fail ,the reason is make directory fail !");
            }
        }
    }else {
        Log.e("getExternalDirectory","getExternalDirectory fail ,the reason is sdCard nonexistence or sdCard mount fail !");
    }
    return appCacheDir;
}

/**
 * 获取内存缓存目录
 * @param type 子目录,可以为空,为空直接返回一级目录
 * @return 缓存目录文件夹 或 null(创建目录文件失败)
 * 注:该方法获取的目录是能供当前应用自己使用,外部应用没有读写权限,如 系统相机应用
 */
public static File getInternalCacheDirectory(Context context,String type) {
    File appCacheDir = null;
    if (TextUtils.isEmpty(type)){
        appCacheDir = context.getCacheDir();// /data/data/app_package_name/cache
    }else {
        appCacheDir = new File(context.getFilesDir(),type);// /data/data/app_package_name/files/type
    }

    if (!appCacheDir.exists()&&!appCacheDir.mkdirs()){
        Log.e("getInternalDirectory","getInternalDirectory fail ,the reason is make directory fail !");
    }
    return appCacheDir;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值