Home | History | Annotate | Download | only in camera
      1 /*
      2  * Copyright (C) 2010 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 com.android.camera;
     18 
     19 import java.io.File;
     20 import java.io.FileOutputStream;
     21 
     22 import android.annotation.TargetApi;
     23 import android.content.ContentResolver;
     24 import android.content.ContentValues;
     25 import android.location.Location;
     26 import android.net.Uri;
     27 import android.os.Build;
     28 import android.os.Environment;
     29 import android.os.StatFs;
     30 import android.provider.MediaStore.Images;
     31 import android.provider.MediaStore.Images.ImageColumns;
     32 import android.provider.MediaStore.MediaColumns;
     33 import android.util.Log;
     34 
     35 import com.android.camera.data.LocalData;
     36 import com.android.camera.exif.ExifInterface;
     37 import com.android.camera.util.ApiHelper;
     38 
     39 public class Storage {
     40     private static final String TAG = "CameraStorage";
     41 
     42     public static final String DCIM =
     43             Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
     44 
     45     public static final String DIRECTORY = DCIM + "/Camera";
     46 
     47     // Match the code in MediaProvider.computeBucketValues().
     48     public static final String BUCKET_ID =
     49             String.valueOf(DIRECTORY.toLowerCase().hashCode());
     50 
     51     public static final long UNAVAILABLE = -1L;
     52     public static final long PREPARING = -2L;
     53     public static final long UNKNOWN_SIZE = -3L;
     54     public static final long LOW_STORAGE_THRESHOLD_BYTES = 50000000;
     55 
     56     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
     57     private static void setImageSize(ContentValues values, int width, int height) {
     58         // The two fields are available since ICS but got published in JB
     59         if (ApiHelper.HAS_MEDIA_COLUMNS_WIDTH_AND_HEIGHT) {
     60             values.put(MediaColumns.WIDTH, width);
     61             values.put(MediaColumns.HEIGHT, height);
     62         }
     63     }
     64 
     65     public static void writeFile(String path, byte[] data) {
     66         FileOutputStream out = null;
     67         try {
     68             out = new FileOutputStream(path);
     69             out.write(data);
     70         } catch (Exception e) {
     71             Log.e(TAG, "Failed to write data", e);
     72         } finally {
     73             try {
     74                 out.close();
     75             } catch (Exception e) {
     76             }
     77         }
     78     }
     79 
     80     // Save the image and add it to media store.
     81     public static Uri addImage(ContentResolver resolver, String title,
     82             long date, Location location, int orientation, ExifInterface exif,
     83             byte[] jpeg, int width, int height) {
     84         // Save the image.
     85         String path = generateFilepath(title);
     86         if (exif != null) {
     87             try {
     88                 exif.writeExif(jpeg, path);
     89             } catch (Exception e) {
     90                 Log.e(TAG, "Failed to write data", e);
     91             }
     92         } else {
     93             writeFile(path, jpeg);
     94         }
     95         return addImage(resolver, title, date, location, orientation,
     96                 jpeg.length, path, width, height);
     97     }
     98 
     99     // Add the image to media store.
    100     public static Uri addImage(ContentResolver resolver, String title,
    101             long date, Location location, int orientation, int jpegLength,
    102             String path, int width, int height) {
    103         // Insert into MediaStore.
    104         ContentValues values = new ContentValues(9);
    105         values.put(ImageColumns.TITLE, title);
    106         values.put(ImageColumns.DISPLAY_NAME, title + ".jpg");
    107         values.put(ImageColumns.DATE_TAKEN, date);
    108         values.put(ImageColumns.MIME_TYPE, LocalData.MIME_TYPE_JPEG);
    109         // Clockwise rotation in degrees. 0, 90, 180, or 270.
    110         values.put(ImageColumns.ORIENTATION, orientation);
    111         values.put(ImageColumns.DATA, path);
    112         values.put(ImageColumns.SIZE, jpegLength);
    113 
    114         setImageSize(values, width, height);
    115 
    116         if (location != null) {
    117             values.put(ImageColumns.LATITUDE, location.getLatitude());
    118             values.put(ImageColumns.LONGITUDE, location.getLongitude());
    119         }
    120 
    121         Uri uri = null;
    122         try {
    123             uri = resolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
    124         } catch (Throwable th)  {
    125             // This can happen when the external volume is already mounted, but
    126             // MediaScanner has not notify MediaProvider to add that volume.
    127             // The picture is still safe and MediaScanner will find it and
    128             // insert it into MediaProvider. The only problem is that the user
    129             // cannot click the thumbnail to review the picture.
    130             Log.e(TAG, "Failed to write MediaStore" + th);
    131         }
    132         return uri;
    133     }
    134 
    135     public static void deleteImage(ContentResolver resolver, Uri uri) {
    136         try {
    137             resolver.delete(uri, null, null);
    138         } catch (Throwable th) {
    139             Log.e(TAG, "Failed to delete image: " + uri);
    140         }
    141     }
    142 
    143     public static String generateFilepath(String title) {
    144         return DIRECTORY + '/' + title + ".jpg";
    145     }
    146 
    147     public static long getAvailableSpace() {
    148         String state = Environment.getExternalStorageState();
    149         Log.d(TAG, "External storage state=" + state);
    150         if (Environment.MEDIA_CHECKING.equals(state)) {
    151             return PREPARING;
    152         }
    153         if (!Environment.MEDIA_MOUNTED.equals(state)) {
    154             return UNAVAILABLE;
    155         }
    156 
    157         File dir = new File(DIRECTORY);
    158         dir.mkdirs();
    159         if (!dir.isDirectory() || !dir.canWrite()) {
    160             return UNAVAILABLE;
    161         }
    162 
    163         try {
    164             StatFs stat = new StatFs(DIRECTORY);
    165             return stat.getAvailableBlocks() * (long) stat.getBlockSize();
    166         } catch (Exception e) {
    167             Log.i(TAG, "Fail to access external storage", e);
    168         }
    169         return UNKNOWN_SIZE;
    170     }
    171 
    172     /**
    173      * OSX requires plugged-in USB storage to have path /DCIM/NNNAAAAA to be
    174      * imported. This is a temporary fix for bug#1655552.
    175      */
    176     public static void ensureOSXCompatible() {
    177         File nnnAAAAA = new File(DCIM, "100ANDRO");
    178         if (!(nnnAAAAA.exists() || nnnAAAAA.mkdirs())) {
    179             Log.e(TAG, "Failed to create " + nnnAAAAA.getPath());
    180         }
    181     }
    182 }
    183