Home | History | Annotate | Download | only in devcamera
      1 /*
      2  * Copyright (C) 2016 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 package com.android.devcamera;
     17 
     18 import android.content.ContentResolver;
     19 import android.content.ContentValues;
     20 import android.content.Context;
     21 import android.content.SharedPreferences;
     22 import android.os.SystemClock;
     23 import android.provider.MediaStore;
     24 import android.util.Log;
     25 
     26 import java.io.File;
     27 import java.io.FileOutputStream;
     28 import java.io.IOException;
     29 import java.io.OutputStream;
     30 
     31 /**
     32  * This class has methods required to save a JPEG to disk as well as update the
     33  * MediaStore database.
     34  */
     35 
     36 
     37 public class MediaSaver {
     38     private static final String TAG = "Snappy_MediaSaver";
     39     private static final String MY_PREFS_NAME = "SnappyPrefs";
     40 
     41     // MediaStore is slow/broken
     42     private static final boolean UDPATE_MEDIA_STORE = true;
     43 
     44 
     45     public static int getNextInt(Context context) {
     46         SharedPreferences prefs = context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
     47         int i = prefs.getInt("counter", 1);
     48         SharedPreferences.Editor editor = prefs.edit();
     49         editor.putInt("counter", i+1);
     50         editor.commit();
     51         return i;
     52     }
     53 
     54     /**
     55      * @param context Application context.
     56      * @param jpegData JPEG byte stream.
     57      */
     58     public static String saveJpeg(Context context, byte[] jpegData, ContentResolver resolver) {
     59         String filename = "";
     60         try {
     61             File file;
     62             while (true) {
     63                 int i = getNextInt(context);
     64                 filename = String.format("/sdcard/DCIM/Camera/SNAP_%05d.JPG", i);
     65                 file = new File(filename);
     66                 if (file.createNewFile()) {
     67                     break;
     68                 }
     69             }
     70 
     71             long t0 = SystemClock.uptimeMillis();
     72             OutputStream os = new FileOutputStream(file);
     73             os.write(jpegData);
     74             os.flush();
     75             os.close();
     76             long t1 = SystemClock.uptimeMillis();
     77 
     78             // update MediaStore so photos apps can find photos right away.
     79             if (UDPATE_MEDIA_STORE) {
     80                 // really slow for some reason: MediaStore.Images.Media.insertImage(resolver, file.getAbsolutePath(), file.getName(), file.getName());
     81                 insertImage(resolver, file);
     82             }
     83             long t2 = SystemClock.uptimeMillis();
     84 
     85             Log.v(TAG, String.format("Wrote JPEG %d bytes as %s in %.3f seconds; mediastore update = %.3f secs",
     86                     jpegData.length, file, (t1 - t0) * 0.001, (t2 - t1) * 0.001)    );
     87         } catch (IOException e) {
     88             Log.e(TAG, "Error creating new file: ", e);
     89         }
     90         return filename;
     91     }
     92 
     93 
     94     // We use this instead of MediaStore.Images.Media.insertImage() because we want to add date metadata
     95     public static void insertImage(ContentResolver cr, File file) {
     96 
     97         ContentValues values = new ContentValues();
     98         values.put(MediaStore.Images.Media.TITLE, file.getName());
     99         values.put(MediaStore.Images.Media.DISPLAY_NAME, file.getName());
    100         values.put(MediaStore.Images.Media.DESCRIPTION, file.getName());
    101         values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    102         values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
    103         // Add the date meta data to ensure the image is added at the front of the gallery
    104         values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
    105         values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
    106 
    107         try {
    108             cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    109         } catch (Exception e) {
    110             Log.w(TAG, "Error updating media store for  " + file, e);
    111         }
    112     }
    113 
    114 }
    115