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 
17 package android.renderscript.cts.refocus;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.graphics.Bitmap;
22 import android.net.Uri;
23 import android.os.Build;
24 import android.os.Environment;
25 
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 
31 /**
32  * Utility class to save images into android image database
33  */
34 public class MediaStoreSaver {
savePNG(Bitmap bitmap, String folderName, String imageName, Context mContext)35     public static final String savePNG(Bitmap bitmap,
36                                        String folderName,
37                                        String imageName,
38                                        Context mContext) {
39         File picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
40         if (!picDir.exists() && picDir.mkdirs()) {
41             // The Pictures directory does not exist on an x86 emulator
42             picDir = mContext.getFilesDir();
43         }
44 
45         File dir = new File(picDir, folderName);
46         if (!dir.exists() && !dir.mkdirs()) {
47             return "";
48         }
49 
50         try {
51             File file = File.createTempFile(imageName, ".png", dir);
52             FileOutputStream fOut = new FileOutputStream(file);
53             bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut);
54             android.util.Log.v("RefocusTest", "saved image: " + file.getAbsolutePath());
55             fOut.flush();
56             fOut.close();
57             return file.getAbsolutePath();
58         } catch (FileNotFoundException e) {
59             e.printStackTrace();
60         } catch (IOException e) {
61             e.printStackTrace();
62         }
63 
64         return "";
65     }
66 }
67