Android创建文件操作

1、创建内部存储缓存文件

 /**

     * Returns the absolute path to the cache file on the filesystem. These
     * files will be
     * ones that get deleted first when the device runs on storage.
     * @param context
     *            Global information about an application environment
     * @param name
     *            File name
     * @return Returns the absolute path to the application specific cache
     *         directory on the
     *         filesystem. Returns null if external storage is not currently
     *         mounted.
     */
    public static File getInternalCacheFile(Context context, String name) {
        File dir = context.getCacheDir();
        if (dir == null) {
            return null;
        }
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return new File(context.getCacheDir(), name);
    }

2、创建外部存储缓存文件

/**
     * Returns the absolute path to the cache file on the external filesystem
     * @param context
     *            Global information about an application environment
     * @param name
     *            File name
     * @return Returns the path of the cache file on external storage. Returns
     *         null if
     *         external storage is not currently mounted.
     */
    public static File getExternalCacheFile(Context context, String name) {
        File dir = getExternalCacheDir(context);
        if (dir == null) {
            return null;
        }
        return new File(getExternalCacheDir(context), name);
    }


 public static File getExternalCacheDir(Context context) {

        //判断是否挂载外部存储
        boolean isSDCardExist = Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
        if (!isSDCardExist) {
            return null;
        }
        final File externalCacheDir = new File(
                Environment.getExternalStorageDirectory(), "/Android/data/"
                        + context.getPackageName() + "/cache/");
        if (!externalCacheDir.exists()) {
            externalCacheDir.mkdirs();
        }
        return externalCacheDir;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值