Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Bitmap; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class Main { public static boolean save(String fileName, Bitmap bitmap) { if (fileName == null || bitmap == null) { return false; } boolean savedSuccessfully = false; OutputStream os = null; File imageFile = new File(fileName); File tmpFile = new File(imageFile.getAbsolutePath() + ".tmp"); try { if (!imageFile.getParentFile().exists()) { imageFile.getParentFile().mkdirs(); } os = new BufferedOutputStream(new FileOutputStream(tmpFile)); savedSuccessfully = bitmap.compress(Bitmap.CompressFormat.PNG, 100, os); } catch (Exception e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (savedSuccessfully && tmpFile != null && !tmpFile.renameTo(imageFile)) { savedSuccessfully = false; } if (!savedSuccessfully) { tmpFile.delete(); } } return savedSuccessfully; } }