android 保存bitmap到指定文件

来源:互联网 发布:js 720度全景 编辑:程序博客网 时间:2024/06/07 15:08

//保存bitmap到指定文件。

    public static void saveBitmap(Bitmap bmp, String targetPath) {

        File file = new File(targetPath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            saveBitmap(bmp, new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void saveBitmap(Bitmap bmp, FileOutputStream fos) {
        try {
            //80 is the compress quality
            bmp.compress(Bitmap.CompressFormat.JPEG, 80, fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
0 0
原创粉丝点击