Android将Bitmap 以图片形式保存到sd卡中

来源:互联网 发布:药智数据库官网 编辑:程序博客网 时间:2024/05/18 13:44
private void saveBitmap2File(Bitmap b, String bitName) throws IOException{File f = new File(getRootFilePath() + bitName + ".png");        f.createNewFile();        FileOutputStream fOut = null;        try {            fOut = new FileOutputStream(f);        } catch (FileNotFoundException e) {            e.printStackTrace();        }        b.compress(Bitmap.CompressFormat.PNG, 100, fOut);        try {            fOut.flush();        } catch (IOException e) {            e.printStackTrace();        }        try {            fOut.close();        } catch (IOException e) {            e.printStackTrace();        }}private boolean hasSDCard() {String status = Environment.getExternalStorageState();if (!status.equals(Environment.MEDIA_MOUNTED)) {return false;}return true;}private String getRootFilePath() {if (hasSDCard()) {return Environment.getExternalStorageDirectory().getAbsolutePath()+ "/";// filePath:/sdcard/} else {return Environment.getDataDirectory().getAbsolutePath() + "/data/"; // filePath:// /data/data/}} 


在主函数中调用方法如下:

try {saveBitmap2File(b,"ZDSharePicNoIconWhiteBottom");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}


0 0
原创粉丝点击