Android文件存储之路

175 阅读1分钟

场景:最近项目需求,需要文件统一管理,主要文件资源有图片、录音、视频等。需要将本地文件新建一份放在指定文件夹下。

1.图片存储

参数:
path:新存储图片路径
imgName:原图片名字
bitmap:原本地图片(由路径转化成bitmap方法放在最下面)

  private static String saveImgToFile(String path, String imgName, Bitmap bitmap) {
    try {
        File file = new File(path, imgName);
        if (!file.exists()) {
          file.getParentFile().mkdirs();//创建文件目录
          file.createNewFile();//创建文件filename,只创建文件,不创建文件夹
        }
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return path + imgName;
}

2.录音存储

参数:

oldPath:原文件绝对地址
path:新地址文件
fileName:文件名称

private static String createVoice(String oldPath, String path, String fileName) {

   try {

    File file = new File(path, fileName);
    if (!file.exists()) {
        file.getParentFile().mkdirs();//创建文件目录
        file.createNewFile();//创建文件filename,只创建文件,不创建文件夹
    }
    InputStream fosfrom = null;
    fosfrom = new FileInputStream(oldPath);
    OutputStream fosto = new FileOutputStream(file);
    byte[] bt = new byte[1024];
    int c;
    while ((c = fosfrom.read(bt)) > 0) {
        fosto.write(bt, 0, c);
    }
    fosfrom.close();
    fosto.close();
    return path + fileName;
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
    return "";
}

3.视频存储

参数:
oldPath:原存储图片路径
path:新文件夹路径
fileName:文件名字

private static String createVideo(String oldPath,String path, String fileName) {
    File file = new File(path, fileName);
    if(!file.exists()){
        file.mkdirs();
    }
    try {
        File file = new File(path, fileName);
        if (!file.exists()) {
        file.getParentFile().mkdirs();
        file.createNewFile();
       }
        InputStream inputStream=new FileInputStream(oldPath);
        FileOutputStream fos = new FileOutputStream(file);

        BufferedInputStream bis = new BufferedInputStream(inputStream);
        byte[] buffer = new byte[1024];
        int len;
        int total = 0;
        while ((len = bis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
            total += len;
        }
        fos.close();
        bis.close();
        inputStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file.getPath();
}

将本地文件根据路径转换为Bitmap

private static Bitmap getDiskBitmap(String path) {
    Bitmap bitmap = null;
    if (TextUtils.isEmpty(path)) {
        return bitmap;
    }
    File file = new File(path);
    if (file.exists()) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inInputShareable = true;
        bitmap = BitmapFactory.decodeFile(path, options);
    }
    return bitmap;
}