关于图片的工具类

/**
*构建路径,
*若路径不存在
*创建路径
*创建文件
*若同名文件存在
*删除同名文件
*创建新的文件
*新建文件输出流
*图片以指定格式压缩入流
*出流
*关流
*/
public static String saveImgToDir(Bitmap b, String name, String toDir) throws Exception {
    //String path = context.getExternalFilesDir(null) + toDir;外部存储中的私有文件
    //String path = context.getFilesDir() + toDir;内部存储中的私有文件
    /*String path = Environment.getExternalStorageDirectory().getPath()外部存储中的公开文件
            + toDir;*/
    //外部存储中的公开专用文件
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()
            + toDir;
    if (!new File(path).exists()) {
        new File(path).mkdirs();
    }
    File mediaFile = new File(path + "/" + name + ".jpg");
    if (mediaFile.exists()) {
        mediaFile.delete();
    }
    mediaFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(mediaFile);
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
    //b.recycle();
    System.gc();
    return mediaFile.getPath();
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容