Java tutorial
//package com.java2s; import android.content.Context; import android.graphics.Bitmap; import android.util.Log; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static final String TAG = "Save"; private static final int CACHE_LIMIT = 1024; public static void saveThumb(Context context, String thumbName, Bitmap image) { if (thumbName == null || thumbName.length() <= 0) return; File pictureFile = getOutputMediaFile(context, thumbName); File dir = new File(context.getCacheDir() + "/thumbnails"); if (dir.exists())//too many caches { File files[] = dir.listFiles(); if (files.length > CACHE_LIMIT) files[0].deleteOnExit(); } if (pictureFile == null) { Log.d(TAG, "Error creating media file, check storage permissions: ");// e.getMessage()); return; } try { FileOutputStream fos = new FileOutputStream(pictureFile); image.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.close(); } catch (FileNotFoundException e) { Log.d(TAG, "File not found: " + e.getMessage()); } catch (IOException e) { Log.d(TAG, "Error accessing file: " + e.getMessage()); } } private static File getOutputMediaFile(Context context, String thumbName) { File file = new File(context.getCacheDir() + "/thumbnails/" + thumbName + ".png"); if (!file.exists()) { File mediaStorageDir = new File(context.getCacheDir() + "/thumbnails"); // Create the storage directory if it does not exist if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { return null; } } // Create a media file name String mImageName = thumbName + ".png"; File mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName); return mediaFile; } else { return file; } } }