1package com.android.ex.carousel;
2
3import java.io.File;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.OutputStream;
7
8import android.content.Context;
9import android.graphics.Bitmap;
10import android.media.MediaScannerConnection;
11import android.os.Environment;
12import android.util.Log;
13
14public class CarouselViewUtilities {
15    /**
16     * Debug utility to write the given bitmap to a file.
17     *
18     * @param context calling context
19     * @param bitmap the bitmap to write
20     * @param filename the name of the file to write
21     * @return
22     */
23    public static boolean writeBitmapToFile(Context context, Bitmap bitmap, String filename) {
24        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
25        File file = new File(path, filename);
26        boolean result = false;
27        try {
28            path.mkdirs();
29            OutputStream os = new FileOutputStream(file);
30            MediaScannerConnection.scanFile(context, new String[] { file.toString() }, null, null);
31            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
32            result = true;
33        } catch (IOException e) {
34            Log.w("ExternalStorage", "Error writing " + file, e);
35        }
36        return result;
37    }
38
39}
40