• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content.ContentResolver;
20 import android.content.ContentValues;
21 import android.location.Location;
22 import android.net.Uri;
23 import android.os.Environment;
24 import android.os.StatFs;
25 import android.provider.MediaStore.Images;
26 import android.provider.MediaStore.Images.ImageColumns;
27 import android.util.Log;
28 
29 import java.io.File;
30 import java.io.FileOutputStream;
31 
32 public class Storage {
33     private static final String TAG = "CameraStorage";
34 
35     public static final String DCIM =
36             Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
37 
38     public static final String DIRECTORY = DCIM + "/Camera";
39 
40     // Match the code in MediaProvider.computeBucketValues().
41     public static final String BUCKET_ID =
42             String.valueOf(DIRECTORY.toLowerCase().hashCode());
43 
44     public static final long UNAVAILABLE = -1L;
45     public static final long PREPARING = -2L;
46     public static final long UNKNOWN_SIZE = -3L;
47     public static final long LOW_STORAGE_THRESHOLD= 50000000;
48     public static final long PICTURE_SIZE = 1500000;
49 
50     private static final int BUFSIZE = 4096;
51 
addImage(ContentResolver resolver, String title, long date, Location location, int orientation, byte[] jpeg, int width, int height)52     public static Uri addImage(ContentResolver resolver, String title, long date,
53                 Location location, int orientation, byte[] jpeg, int width, int height) {
54         // Save the image.
55         String path = DIRECTORY + '/' + title + ".jpg";
56         FileOutputStream out = null;
57         try {
58             out = new FileOutputStream(path);
59             out.write(jpeg);
60         } catch (Exception e) {
61             Log.e(TAG, "Failed to write image", e);
62             return null;
63         } finally {
64             try {
65                 out.close();
66             } catch (Exception e) {
67             }
68         }
69 
70         // Insert into MediaStore.
71         ContentValues values = new ContentValues(9);
72         values.put(ImageColumns.TITLE, title);
73         values.put(ImageColumns.DISPLAY_NAME, title + ".jpg");
74         values.put(ImageColumns.DATE_TAKEN, date);
75         values.put(ImageColumns.MIME_TYPE, "image/jpeg");
76         values.put(ImageColumns.ORIENTATION, orientation);
77         values.put(ImageColumns.DATA, path);
78         values.put(ImageColumns.SIZE, jpeg.length);
79         values.put(ImageColumns.WIDTH, width);
80         values.put(ImageColumns.HEIGHT, height);
81 
82         if (location != null) {
83             values.put(ImageColumns.LATITUDE, location.getLatitude());
84             values.put(ImageColumns.LONGITUDE, location.getLongitude());
85         }
86 
87         Uri uri = resolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
88         if (uri == null) {
89             Log.e(TAG, "Failed to write MediaStore");
90             return null;
91         }
92         return uri;
93     }
94 
getAvailableSpace()95     public static long getAvailableSpace() {
96         String state = Environment.getExternalStorageState();
97         Log.d(TAG, "External storage state=" + state);
98         if (Environment.MEDIA_CHECKING.equals(state)) {
99             return PREPARING;
100         }
101         if (!Environment.MEDIA_MOUNTED.equals(state)) {
102             return UNAVAILABLE;
103         }
104 
105         File dir = new File(DIRECTORY);
106         dir.mkdirs();
107         if (!dir.isDirectory() || !dir.canWrite()) {
108             return UNAVAILABLE;
109         }
110 
111         try {
112             StatFs stat = new StatFs(DIRECTORY);
113             return stat.getAvailableBlocks() * (long) stat.getBlockSize();
114         } catch (Exception e) {
115             Log.i(TAG, "Fail to access external storage", e);
116         }
117         return UNKNOWN_SIZE;
118     }
119 
120     /**
121      * OSX requires plugged-in USB storage to have path /DCIM/NNNAAAAA to be
122      * imported. This is a temporary fix for bug#1655552.
123      */
ensureOSXCompatible()124     public static void ensureOSXCompatible() {
125         File nnnAAAAA = new File(DCIM, "100ANDRO");
126         if (!(nnnAAAAA.exists() || nnnAAAAA.mkdirs())) {
127             Log.e(TAG, "Failed to create " + nnnAAAAA.getPath());
128         }
129     }
130 }
131