From 77ef3234e641170e5f0046bc41800087144a4b14 Mon Sep 17 00:00:00 2001 From: Michael Aaron Safyan Date: Sat, 13 Dec 2014 00:34:38 -0800 Subject: [PATCH] Clarify format used in insertImage This change makes the format used by insertImage() explicit, allowing code that requires this information to depend on it symbolically rather than by examining the source. A case where this is useful is when specifying the MIME type in Intent.setType() when passing the result of insertImage as an Intent extra and attempting to invoke an activity that handles most "image/*" MIME types but that does not have a registered intent filter for "vnd.android.cursor.dir/image". In such a scenario, code might have hard-coded "image/jpeg" as a string; after this change, it becomes possible to use the STORED_CONTENT_TYPE variable, which would make such code less fragile. --- core/java/android/provider/MediaStore.java | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/core/java/android/provider/MediaStore.java b/core/java/android/provider/MediaStore.java index 4e016723..45a901a3 100644 --- a/core/java/android/provider/MediaStore.java +++ b/core/java/android/provider/MediaStore.java @@ -778,7 +778,8 @@ private static final Bitmap StoreThumbnail( } /** - * Insert an image and create a thumbnail for it. + * Insert an image and create a thumbnail for it. The image is encoded using a default + * format indicated by {@code STORED_COMPRESSION_FORMAT} and with a default quality parameter. * * @param cr The content resolver to use * @param source The stream to use for the image @@ -792,7 +793,7 @@ public static final String insertImage(ContentResolver cr, Bitmap source, ContentValues values = new ContentValues(); values.put(Images.Media.TITLE, title); values.put(Images.Media.DESCRIPTION, description); - values.put(Images.Media.MIME_TYPE, "image/jpeg"); + values.put(Images.Media.MIME_TYPE, STORED_CONTENT_TYPE); Uri url = null; String stringUrl = null; /* value to be returned */ @@ -803,7 +804,7 @@ public static final String insertImage(ContentResolver cr, Bitmap source, if (source != null) { OutputStream imageOut = cr.openOutputStream(url); try { - source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut); + source.compress(STORED_COMPRESSION_FORMAT, 50, imageOut); } finally { imageOut.close(); } @@ -861,12 +862,21 @@ public static Uri getContentUri(String volumeName) { getContentUri("external"); /** - * The MIME type of of this directory of - * images. Note that each entry in this directory will have a standard - * image MIME type as appropriate -- for example, image/jpeg. + * The MIME type of this directory of images. Note that each entry in this directory will have + * a standard image MIME type as specified by STORED_CONTENT_TYPE. */ public static final String CONTENT_TYPE = "vnd.android.cursor.dir/image"; + /** + * The MIME type of the underlying data when stored. + */ + public static final String STORED_CONTENT_TYPE = "image/jpeg"; + + /** + * The compression format used to compress the images stored in this media store. + */ + public static final Bitmap.CompressFormat STORED_COMPRESSION_FORMAT = Bitmap.CompressFormat.JPEG; + /** * The default sort order for this table */