---
title: "将bitmap保存为文件"
author: "googlelee"
date: "2012-05-21T10:37:24Z"
tags: []
categories: ["移动开发"]
slug: "bitmap"
draft: false
lastmod: "2012-05-21T10:37:24Z"
id: 1536068
url: "https://googlelee.iteye.com/blog/1536068"
---

private void saveBmpToSd(Bitmap bm, String url) {

        if (bm == null || url==null) { 

            return;

        }

        if (10 &gt;freeSpaceOnSd()) {

            return;

        }

 

        String filename = url.replace("/", "").replace(":", "").replace(",", "")

        .replace("\\\\", "").replace(".", "").replace("?", "").replace("|", "").replace("\\"", "")

        .replace("&gt;", "").replace("&lt;", "")+".png";

 

        String dir = this.getExternalCacheDir().getAbsolutePath();

        File file = new File(dir +"/" + filename);

 

        if(!file.exists()) {

       try {

           file.createNewFile();

           OutputStream outStream = new FileOutputStream(file);

           bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);

           outStream.flush();

           outStream.close();

       } catch (IOException e) {

       

       }

        }

        if(StringUtils.IsShowLog) {

        //StringUtils.log(tag, "filename="+filename);

        //StringUtils.log(tag, "dir="+dir);

        }

    }