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 block
e.printStackTrace();
}