MediaStoreSaver.java revision 89e35a551ee4b0bfcb3ede10da3b027dba1da7ef
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.rs.refocus;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.Bitmap;
22import android.net.Uri;
23import android.os.Build;
24import android.os.Environment;
25
26import java.io.File;
27import java.io.FileNotFoundException;
28import java.io.FileOutputStream;
29import java.io.IOException;
30
31/**
32 * Utility class to save images into android image database
33 */
34public class MediaStoreSaver {
35    public static final String savePNG(Bitmap bitmap,
36                                       String folderName,
37                                       String imageName,
38                                        Context mContext) {
39        File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
40        String folder_path = folder.getAbsolutePath();
41        String file_path = folder_path + "/" + folderName;
42        File dir = new File(file_path);
43
44        if (!dir.exists()) {
45            System.out.println("make directory: " + dir.getAbsolutePath());
46            dir.mkdirs();
47        }
48        //int n = dir.listFiles().length;
49        //File file = new File(dir, imageName + n + ".png");
50        File file = new File(dir, imageName + ".png");
51        try {
52            FileOutputStream fOut = new FileOutputStream(file);
53            bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut);
54            System.out.println("saved image: " + file.getAbsolutePath());
55            fOut.flush();
56            fOut.close();
57        } catch (FileNotFoundException e) {
58            e.printStackTrace();
59        } catch (IOException e) {
60            e.printStackTrace();
61        }
62
63        // MediaStorageScan(mContext, file);
64        return file.getAbsolutePath();
65    }
66    /*
67     * Refresh image files to view them on computer
68     */
69    private static void MediaStorageScan(Context context, final File file) {
70        final Uri fileUri = Uri.fromFile(file);
71
72        // New way
73        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
74            context.sendBroadcast(new Intent("android.hardware.action.NEW_PICTURE", fileUri));
75        }
76
77        // Keep compatibility
78        context.sendBroadcast(new Intent("com.android.camera.NEW_PICTURE", fileUri));
79
80        // Usual way
81        final Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
82        intent.setData(fileUri);
83        context.sendBroadcast(intent);
84    }
85
86}
87