From 7cfcafdf8f4a439c8fa87b612616fe409979e8a4 Mon Sep 17 00:00:00 2001 From: Ruben Brunk Date: Thu, 17 Oct 2013 15:41:44 -0700 Subject: gcam: Add placeholder image. Bug: 11050749 Change-Id: I374c5919d6da0609fccd21c09775fa91894d5a24 --- src/com/android/camera/Storage.java | 98 ++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 22 deletions(-) (limited to 'src/com/android/camera/Storage.java') diff --git a/src/com/android/camera/Storage.java b/src/com/android/camera/Storage.java index a8ce08b93..499d03029 100644 --- a/src/com/android/camera/Storage.java +++ b/src/com/android/camera/Storage.java @@ -43,6 +43,7 @@ public class Storage { Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString(); public static final String DIRECTORY = DCIM + "/Camera"; + public static final String JPEG_POSTFIX = ".jpg"; // Match the code in MediaProvider.computeBucketValues(). public static final String BUCKET_ID = @@ -62,6 +63,18 @@ public class Storage { } } + public static void writeFile(String path, byte[] jpeg, ExifInterface exif) { + if (exif != null) { + try { + exif.writeExif(jpeg, path); + } catch (Exception e) { + Log.e(TAG, "Failed to write data", e); + } + } else { + writeFile(path, jpeg); + } + } + public static void writeFile(String path, byte[] data) { FileOutputStream out = null; try { @@ -73,39 +86,41 @@ public class Storage { try { out.close(); } catch (Exception e) { + Log.e(TAG, "Failed to close file after write", e); } } } - // Save the image and add it to media store. - public static Uri addImage(ContentResolver resolver, String title, - long date, Location location, int orientation, ExifInterface exif, - byte[] jpeg, int width, int height) { - // Save the image. + // Save the image and add it to the MediaStore. + public static Uri addImage(ContentResolver resolver, String title, long date, + Location location, int orientation, ExifInterface exif, byte[] jpeg, int width, + int height) { + + return addImage(resolver, title, date, location, orientation, exif, jpeg, width, height, + LocalData.MIME_TYPE_JPEG); + } + + // Save the image with a given mimeType and add it the MediaStore. + public static Uri addImage(ContentResolver resolver, String title, long date, + Location location, int orientation, ExifInterface exif, byte[] jpeg, int width, + int height, String mimeType) { + String path = generateFilepath(title); - if (exif != null) { - try { - exif.writeExif(jpeg, path); - } catch (Exception e) { - Log.e(TAG, "Failed to write data", e); - } - } else { - writeFile(path, jpeg); - } + writeFile(path, jpeg, exif); return addImage(resolver, title, date, location, orientation, - jpeg.length, path, width, height); + jpeg.length, path, width, height, mimeType); } - // Add the image to media store. - public static Uri addImage(ContentResolver resolver, String title, + // Get a ContentValues object for the given photo data + public static ContentValues getContentValuesForData(String title, long date, Location location, int orientation, int jpegLength, - String path, int width, int height) { - // Insert into MediaStore. - ContentValues values = new ContentValues(9); + String path, int width, int height, String mimeType) { + + ContentValues values = new ContentValues(11); values.put(ImageColumns.TITLE, title); - values.put(ImageColumns.DISPLAY_NAME, title + ".jpg"); + values.put(ImageColumns.DISPLAY_NAME, title + JPEG_POSTFIX); values.put(ImageColumns.DATE_TAKEN, date); - values.put(ImageColumns.MIME_TYPE, LocalData.MIME_TYPE_JPEG); + values.put(ImageColumns.MIME_TYPE, mimeType); // Clockwise rotation in degrees. 0, 90, 180, or 270. values.put(ImageColumns.ORIENTATION, orientation); values.put(ImageColumns.DATA, path); @@ -117,6 +132,17 @@ public class Storage { values.put(ImageColumns.LATITUDE, location.getLatitude()); values.put(ImageColumns.LONGITUDE, location.getLongitude()); } + return values; + } + + // Add the image to media store. + public static Uri addImage(ContentResolver resolver, String title, + long date, Location location, int orientation, int jpegLength, + String path, int width, int height, String mimeType) { + // Insert into MediaStore. + ContentValues values = + getContentValuesForData(title, date, location, orientation, jpegLength, path, + width, height, mimeType); Uri uri = null; try { @@ -132,6 +158,34 @@ public class Storage { return uri; } + // Overwrites the file and updates the MediaStore + public static void updateImage(Uri imageUri, ContentResolver resolver, String title, long date, + Location location, int orientation, ExifInterface exif, byte[] jpeg, int width, + int height, String mimeType) { + String path = generateFilepath(title); + writeFile(path, jpeg, exif); + updateImage(imageUri, resolver, title, date, location, orientation, jpeg.length, path, + width, height, mimeType); + } + + // Updates the image values in MediaStore + public static void updateImage(Uri imageUri, ContentResolver resolver, String title, + long date, Location location, int orientation, int jpegLength, + String path, int width, int height, String mimeType) { + + ContentValues values = + getContentValuesForData(title, date, location, orientation, jpegLength, path, + width, height, mimeType); + + // Update the MediaStore + int rowsModified = resolver.update(imageUri, values, null, null); + if (rowsModified != 1) { + // This should never happen + throw new IllegalStateException("Bad number of rows (" + rowsModified + + ") updated for uri: " + imageUri); + } + } + public static void deleteImage(ContentResolver resolver, Uri uri) { try { resolver.delete(uri, null, null); -- cgit v1.2.3 From 3bf1cda53ebd85152fa9eed3ea8c9652a944df9b Mon Sep 17 00:00:00 2001 From: Ruben Brunk Date: Fri, 15 Nov 2013 15:30:47 -0800 Subject: gcam: Clean up placeholders, and add deletion robustness. Bug: 11708734 Change-Id: I8a017a382f12165be27afbf78e733bcc1fc0ef6f --- src/com/android/camera/Storage.java | 41 +++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'src/com/android/camera/Storage.java') diff --git a/src/com/android/camera/Storage.java b/src/com/android/camera/Storage.java index 499d03029..b09eedafe 100644 --- a/src/com/android/camera/Storage.java +++ b/src/com/android/camera/Storage.java @@ -144,21 +144,11 @@ public class Storage { getContentValuesForData(title, date, location, orientation, jpegLength, path, width, height, mimeType); - Uri uri = null; - try { - uri = resolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values); - } catch (Throwable th) { - // This can happen when the external volume is already mounted, but - // MediaScanner has not notify MediaProvider to add that volume. - // The picture is still safe and MediaScanner will find it and - // insert it into MediaProvider. The only problem is that the user - // cannot click the thumbnail to review the picture. - Log.e(TAG, "Failed to write MediaStore" + th); - } - return uri; + return insertImage(resolver, values); } - // Overwrites the file and updates the MediaStore + // Overwrites the file and updates the MediaStore, or inserts the image if + // one does not already exist. public static void updateImage(Uri imageUri, ContentResolver resolver, String title, long date, Location location, int orientation, ExifInterface exif, byte[] jpeg, int width, int height, String mimeType) { @@ -168,7 +158,8 @@ public class Storage { width, height, mimeType); } - // Updates the image values in MediaStore + // Updates the image values in MediaStore, or inserts the image if one does + // not already exist. public static void updateImage(Uri imageUri, ContentResolver resolver, String title, long date, Location location, int orientation, int jpegLength, String path, int width, int height, String mimeType) { @@ -179,7 +170,12 @@ public class Storage { // Update the MediaStore int rowsModified = resolver.update(imageUri, values, null, null); - if (rowsModified != 1) { + + if (rowsModified == 0) { + // If no prior row existed, insert a new one. + Log.w(TAG, "updateImage called with no prior image at uri: " + imageUri); + insertImage(resolver, values); + } else if (rowsModified != 1) { // This should never happen throw new IllegalStateException("Bad number of rows (" + rowsModified + ") updated for uri: " + imageUri); @@ -233,4 +229,19 @@ public class Storage { Log.e(TAG, "Failed to create " + nnnAAAAA.getPath()); } } + + private static Uri insertImage(ContentResolver resolver, ContentValues values) { + Uri uri = null; + try { + uri = resolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values); + } catch (Throwable th) { + // This can happen when the external volume is already mounted, but + // MediaScanner has not notify MediaProvider to add that volume. + // The picture is still safe and MediaScanner will find it and + // insert it into MediaProvider. The only problem is that the user + // cannot click the thumbnail to review the picture. + Log.e(TAG, "Failed to write MediaStore" + th); + } + return uri; + } } -- cgit v1.2.3