Home | History | Annotate | Download | only in healingbrush
      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 rs.example.android.com.healingbrush;
     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 public class MediaStoreSaver {
     32     public static final byte TYPE_PNG = 2;
     33     public static final byte TYPE_JPG = 3;
     34 
     35     public static final String save(Bitmap bitmap,
     36                                     String folderName,
     37                                     String imageName,
     38                                     Context mContext,
     39                                     byte imageType) {
     40         File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
     41         String folder_path = folder.getAbsolutePath();
     42         String file_path = folder_path + "/" + folderName;
     43         File dir = new File(file_path);
     44 
     45         if (!dir.exists()) {
     46             dir.mkdirs();
     47         }
     48         File file = null;
     49         try {
     50             String suffix = ".png";
     51             Bitmap.CompressFormat format = Bitmap.CompressFormat.PNG;
     52             if (imageType == TYPE_JPG) {
     53                 suffix = ".jpg";
     54                 format = Bitmap.CompressFormat.JPEG;
     55             }
     56 
     57             file = File.createTempFile(imageName, suffix, dir);
     58             FileOutputStream fOut = new FileOutputStream(file);
     59             bitmap.compress(format, 100, fOut);
     60             System.out.println("saved image: " + file.getAbsolutePath());
     61             fOut.flush();
     62             fOut.close();
     63         } catch (FileNotFoundException e) {
     64             e.printStackTrace();
     65         } catch (IOException e) {
     66             e.printStackTrace();
     67         }
     68 
     69         MediaStorageScan(mContext, file);
     70         return file.getAbsolutePath();
     71     }
     72 
     73     /*
     74      * Refresh image files to view them on computer
     75      */
     76     private static void MediaStorageScan(Context context, final File file) {
     77         final Uri fileUri = Uri.fromFile(file);
     78 
     79         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
     80             context.sendBroadcast(new Intent("android.hardware.action.NEW_PICTURE", fileUri));
     81         }
     82 
     83         context.sendBroadcast(new Intent("com.android.camera.NEW_PICTURE", fileUri));
     84 
     85         final Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
     86         intent.setData(fileUri);
     87         context.sendBroadcast(intent);
     88     }
     89 
     90 }
     91