1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.camera;
import com.android.camera.gallery.BaseImageList;
import com.android.camera.gallery.IImage;
import com.android.camera.gallery.IImageList;
import com.android.camera.gallery.ImageList;
import com.android.camera.gallery.ImageListUber;
import com.android.camera.gallery.VideoList;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.location.Location;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.OrientationEventListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
/**
* {@code ImageManager} is used to retrieve and store images
* in the media content provider.
*/
public class ImageManager {
private static final String TAG = "ImageManager";
private static final Uri STORAGE_URI = Images.Media.EXTERNAL_CONTENT_URI;
private static final Uri THUMB_URI
= Images.Thumbnails.EXTERNAL_CONTENT_URI;
private static final Uri VIDEO_STORAGE_URI =
Uri.parse("content://media/external/video/media");
private ImageManager() {
}
/**
* {@code ImageListParam} specifies all the parameters we need to create an
* image list (we also need a ContentResolver).
*/
public static class ImageListParam implements Parcelable {
public DataLocation mLocation;
public int mInclusion;
public int mSort;
public String mBucketId;
// This is only used if we are creating an empty image list.
public boolean mIsEmptyImageList;
public ImageListParam() {
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mLocation.ordinal());
out.writeInt(mInclusion);
out.writeInt(mSort);
out.writeString(mBucketId);
out.writeInt(mIsEmptyImageList ? 1 : 0);
}
private ImageListParam(Parcel in) {
mLocation = DataLocation.values()[in.readInt()];
mInclusion = in.readInt();
mSort = in.readInt();
mBucketId = in.readString();
mIsEmptyImageList = (in.readInt() != 0);
}
public String toString() {
return String.format("ImageListParam{loc=%s,inc=%d,sort=%d," +
"bucket=%s,empty=%b}", mLocation, mInclusion,
mSort, mBucketId, mIsEmptyImageList);
}
public static final Parcelable.Creator CREATOR
= new Parcelable.Creator() {
public ImageListParam createFromParcel(Parcel in) {
return new ImageListParam(in);
}
public ImageListParam[] newArray(int size) {
return new ImageListParam[size];
}
};
public int describeContents() {
return 0;
}
}
// Location
public static enum DataLocation { NONE, INTERNAL, EXTERNAL, ALL }
// Inclusion
public static final int INCLUDE_IMAGES = (1 << 0);
public static final int INCLUDE_VIDEOS = (1 << 2);
// Sort
public static final int SORT_ASCENDING = 1;
public static final int SORT_DESCENDING = 2;
public static final String CAMERA_IMAGE_BUCKET_NAME =
Environment.getExternalStorageDirectory().toString()
+ "/DCIM/Camera";
public static final String CAMERA_IMAGE_BUCKET_ID =
getBucketId(CAMERA_IMAGE_BUCKET_NAME);
/**
* Matches code in MediaProvider.computeBucketValues. Should be a common
* function.
*/
public static String getBucketId(String path) {
return String.valueOf(path.toLowerCase().hashCode());
}
/**
* OSX requires plugged-in USB storage to have path /DCIM/NNNAAAAA to be
* imported. This is a temporary fix for bug#1655552.
*/
public static void ensureOSXCompatibleFolder() {
File nnnAAAAA = new File(
Environment.getExternalStorageDirectory().toString()
+ "/DCIM/100ANDRO");
if ((!nnnAAAAA.exists()) && (!nnnAAAAA.mkdir())) {
Log.e(TAG, "create NNNAAAAA file: " + nnnAAAAA.getPath()
+ " failed");
}
}
public static int roundOrientation(int orientationInput) {
int orientation = orientationInput;
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
orientation = 0;
}
orientation = orientation % 360;
int retVal;
if (orientation < (0 * 90) + 45) {
retVal = 0;
} else if (orientation < (1 * 90) + 45) {
retVal = 90;
} else if (orientation < (2 * 90) + 45) {
retVal = 180;
} else if (orientation < (3 * 90) + 45) {
retVal = 270;
} else {
retVal = 0;
}
return retVal;
}
public static void setImageSize(ContentResolver cr, Uri uri, long size) {
ContentValues values = new ContentValues();
values.put(Images.Media.SIZE, size);
cr.update(uri, values, null, null);
}
//
// Stores a bitmap or a jpeg byte array to a file (using the specified
// directory and filename). Also add an entry to the media store for
// this picture. The title, dateTaken, location are attributes for the
// picture. The degree is a one element array which returns the orientation
// of the picture.
//
public static Uri addImage(ContentResolver cr, String title, long dateTaken,
Location location, String directory, String filename,
Bitmap source, byte[] jpegData, int[] degree) {
// We should store image data earlier than insert it to ContentProvider,
// otherwise we may not be able to generate thumbnail in time.
OutputStream outputStream = null;
String filePath = directory + "/" + filename;
try {
File dir = new File(directory);
if (!dir.exists()) dir.mkdirs();
File file = new File(directory, filename);
outputStream = new FileOutputStream(file);
if (source != null) {
source.compress(CompressFormat.JPEG, 75, outputStream);
degree[0] = 0;
} else {
outputStream.write(jpegData);
degree[0] = getExifOrientation(filePath);
}
} catch (FileNotFoundException ex) {
Log.w(TAG, ex);
return null;
} catch (IOException ex) {
Log.w(TAG, ex);
return null;
} finally {
Util.closeSilently(outputStream);
}
ContentValues values = new ContentValues(7);
values.put(Images.Media.TITLE, title);
// That filename is what will be handed to Gmail when a user shares a
// photo. Gmail gets the name of the picture attachment from the
// "DISPLAY_NAME" field.
values.put(Images.Media.DISPLAY_NAME, filename);
values.put(Images.Media.DATE_TAKEN, dateTaken);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.ORIENTATION, degree[0]);
values.put(Images.Media.DATA, filePath);
if (location != null) {
values.put(Images.Media.LATITUDE, location.getLatitude());
values.put(Images.Media.LONGITUDE, location.getLongitude());
}
return cr.insert(STORAGE_URI, values);
}
public static int getExifOrientation(String filepath) {
int degree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
} catch (IOException ex) {
Log.e(TAG, "cannot read exif", ex);
}
if (exif != null) {
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
if (orientation != -1) {
// We only recognize a subset of orientation tag values.
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
}
}
return degree;
}
// This is the factory function to create an image list.
public static IImageList makeImageList(ContentResolver cr,
ImageListParam param) {
DataLocation location = param.mLocation;
int inclusion = param.mInclusion;
int sort = param.mSort;
String bucketId = param.mBucketId;
boolean isEmptyImageList = param.mIsEmptyImageList;
if (isEmptyImageList || cr == null) {
return new EmptyImageList();
}
// false ==> don't require write access
boolean haveSdCard = hasStorage(false);
// use this code to merge videos and stills into the same list
ArrayList<BaseImageList> l = new ArrayList<BaseImageList>();
if (haveSdCard && location != DataLocation.INTERNAL) {
if ((inclusion & INCLUDE_IMAGES) != 0) {
l.add(new ImageList(cr, STORAGE_URI, sort, bucketId));
}
if ((inclusion & INCLUDE_VIDEOS) != 0) {
l.add(new VideoList(cr, VIDEO_STORAGE_URI, sort, bucketId));
}
}
if (location == DataLocation.INTERNAL || location == DataLocation.ALL) {
if ((inclusion & INCLUDE_IMAGES) != 0) {
l.add(new ImageList(cr,
Images.Media.INTERNAL_CONTENT_URI, sort, bucketId));
}
}
// Optimization: If some of the lists are empty, remove them.
// If there is only one remaining list, return it directly.
Iterator<BaseImageList> iter = l.iterator();
while (iter.hasNext()) {
BaseImageList sublist = iter.next();
if (sublist.isEmpty()) {
sublist.close();
iter.remove();
}
}
if (l.size() == 1) {
BaseImageList list = l.get(0);
return list;
}
ImageListUber uber = new ImageListUber(
l.toArray(new IImageList[l.size()]), sort);
return uber;
}
private static class EmptyImageList implements IImageList {
public void close() {
}
public int getCount() {
return 0;
}
public IImage getImageAt(int i) {
return null;
}
}
public static ImageListParam getImageListParam(DataLocation location,
int inclusion, int sort, String bucketId) {
ImageListParam param = new ImageListParam();
param.mLocation = location;
param.mInclusion = inclusion;
param.mSort = sort;
param.mBucketId = bucketId;
return param;
}
public static IImageList makeImageList(ContentResolver cr,
DataLocation location, int inclusion, int sort, String bucketId) {
ImageListParam param = getImageListParam(location, inclusion, sort,
bucketId);
return makeImageList(cr, param);
}
private static boolean checkFsWritable() {
// Create a temporary file to see whether a volume is really writeable.
// It's important not to put it in the root directory which may have a
// limit on the number of files.
String directoryName =
Environment.getExternalStorageDirectory().toString() + "/DCIM";
File directory = new File(directoryName);
if (!directory.isDirectory()) {
if (!directory.mkdirs()) {
return false;
}
}
File f = new File(directoryName, ".probe");
try {
// Remove stale file if any
if (f.exists()) {
f.delete();
}
if (!f.createNewFile()) {
return false;
}
f.delete();
return true;
} catch (IOException ex) {
return false;
}
}
public static boolean hasStorage() {
return hasStorage(true);
}
public static boolean hasStorage(boolean requireWriteAccess) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
return writable;
} else {
return true;
}
} else if (!requireWriteAccess
&& Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
private static Cursor query(ContentResolver resolver, Uri uri,
String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
try {
if (resolver == null) {
return null;
}
return resolver.query(
uri, projection, selection, selectionArgs, sortOrder);
} catch (UnsupportedOperationException ex) {
return null;
}
}
public static boolean isMediaScannerScanning(ContentResolver cr) {
boolean result = false;
Cursor cursor = query(cr, MediaStore.getMediaScannerUri(),
new String [] {MediaStore.MEDIA_SCANNER_VOLUME},
null, null, null);
if (cursor != null) {
if (cursor.getCount() == 1) {
cursor.moveToFirst();
result = "external".equals(cursor.getString(0));
}
cursor.close();
}
return result;
}
public static String getLastImageThumbPath() {
return Environment.getExternalStorageDirectory().toString() +
"/DCIM/.thumbnails/image_last_thumb";
}
public static String getLastVideoThumbPath() {
return Environment.getExternalStorageDirectory().toString() +
"/DCIM/.thumbnails/video_last_thumb";
}
public static String getTempJpegPath() {
return Environment.getExternalStorageDirectory().toString() +
"/DCIM/.tempjpeg";
}
}
|