Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.graphics.Bitmap; import android.net.Uri; import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; public class Main { /** * Write the given bitmap to the given uri using the given compression. */ static void writeBitmapToUri(Context context, Bitmap bitmap, Uri uri, Bitmap.CompressFormat compressFormat, int compressQuality) throws FileNotFoundException { OutputStream outputStream = null; try { outputStream = context.getContentResolver().openOutputStream(uri); bitmap.compress(compressFormat, compressQuality, outputStream); } finally { closeSafe(outputStream); } } /** * Close the given closeable object (Stream) in a safe way: check if it is null and catch-log * exception thrown. * * @param closeable the closable object to close */ private static void closeSafe(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException ignored) { } } } }