summaryrefslogtreecommitdiffstats
path: root/carousel/java/com/android/ex/carousel/CarouselViewUtilities.java
blob: 8b5373449dd6f21323732386de6f0cdb389faafc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.android.ex.carousel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.util.Log;

public class CarouselViewUtilities {
    /**
     * Debug utility to write the given bitmap to a file.
     *
     * @param context calling context
     * @param bitmap the bitmap to write
     * @param filename the name of the file to write
     * @return
     */
    public static boolean writeBitmapToFile(Context context, Bitmap bitmap, String filename) {
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File file = new File(path, filename);
        boolean result = false;
        try {
            path.mkdirs();
            OutputStream os = new FileOutputStream(file);
            MediaScannerConnection.scanFile(context, new String[] { file.toString() }, null, null);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
            result = true;
        } catch (IOException e) {
            Log.w("ExternalStorage", "Error writing " + file, e);
        }
        return result;
    }

}