getCacheDir(),getFilesDir(),getExternalCacheDir(),getExternalFilesDir(),getExternalStorageDirector()

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41659081/article/details/88682832

判断SD卡是否可用

设置->应用->应用详情里面的”清除缓存“与”清除数据“选项
清除缓存:将getCacheDir()和getExternalCacheDir()的数据清空
清除数据:将getCacheDir(),getFilesDir(),getExternalCacheDir(),getExternalFilesDir()数据清空

getCacheDir()和getFilesDir()

这两个方法是内部储存(没有root的手机打不开该文件夹)
context.getCacheDir() 获取的目录形式:data/data/包名/cache
context.getFilesDir() 获取的目录形式:data/data/包名/files

getExternalCacheDir()和getExternalFilesDir()

这两个方法是外部储存
app的私有目录,删除APP后这两个方法创建的文件夹也会一起被删除
context.getExternalCacheDir()获取的目录形式:storage/sdcard/Android/data/包名/cache(storage也可能是mnt)
context.getExternalFilesDir()获取的目录形式:storage/sdcard/Android/data/包名/files(storage也可能是mnt)

getExternalStorageDirector()

Environment.getExternalStorageDirector()获取的目录形式:storage/sdcard(storage也可能是mnt)

判断SD卡是否可用

public static boolean isSDAvailable() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

创建目录

public static File getDir(String cache) {
        StringBuilder path = new StringBuilder();
        if (isSDAvailable()) {
            path.append(Environment.getExternalStorageDirectory().getAbsolutePath());
            path.append(File.separator);
            path.append(ROOT);//第一层目录
            path.append(File.separator);
            path.append(ROOT1);//第二层目录
            path.append(File.separator);
            path.append(cache);

        }else{
            File filesDir = context.getCacheDir();//这边的context可用application的context  
            path.append(filesDir.getAbsolutePath());
            path.append(File.separator);
            path.append(cache);
        }
        String c=path.toString();
        File file = new File(c);
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();// 创建文件夹
        }
        return file;
    }

猜你喜欢

转载自blog.csdn.net/qq_41659081/article/details/88682832