diff options
author | Yuli Huang <yuli@google.com> | 2012-04-10 18:51:36 +0800 |
---|---|---|
committer | Yuli Huang <yuli@google.com> | 2012-04-10 18:51:36 +0800 |
commit | 68767b61aa7b0b2eebf673a05c9e7b7429eb5ee7 (patch) | |
tree | 64c99c589916d88546618b019a7ca7041bd9cd5b /src/com/android/gallery3d/photoeditor | |
parent | d8d1cfdb80a7bcc950e8deb957a6c55ec83a68b0 (diff) | |
download | android_packages_apps_Snap-68767b61aa7b0b2eebf673a05c9e7b7429eb5ee7.tar.gz android_packages_apps_Snap-68767b61aa7b0b2eebf673a05c9e7b7429eb5ee7.tar.bz2 android_packages_apps_Snap-68767b61aa7b0b2eebf673a05c9e7b7429eb5ee7.zip |
Save edited photo in the source photo directory if applicable.
bug:6168430
Change-Id: I97d8cf1d286bc4d5d7c38ba5118b9a952f17b252
Diffstat (limited to 'src/com/android/gallery3d/photoeditor')
-rw-r--r-- | src/com/android/gallery3d/photoeditor/BitmapUtils.java | 8 | ||||
-rw-r--r-- | src/com/android/gallery3d/photoeditor/SaveCopyTask.java | 101 |
2 files changed, 63 insertions, 46 deletions
diff --git a/src/com/android/gallery3d/photoeditor/BitmapUtils.java b/src/com/android/gallery3d/photoeditor/BitmapUtils.java index a3a18d217..9b4114394 100644 --- a/src/com/android/gallery3d/photoeditor/BitmapUtils.java +++ b/src/com/android/gallery3d/photoeditor/BitmapUtils.java @@ -181,15 +181,13 @@ public class BitmapUtils { * Saves the bitmap by given directory, filename, and format; if the directory is given null, * then saves it under the cache directory. */ - public File saveBitmap( - Bitmap bitmap, String directory, String filename, CompressFormat format) { + public File saveBitmap(Bitmap bitmap, File directory, String filename, CompressFormat format) { if (directory == null) { - directory = context.getCacheDir().getAbsolutePath(); + directory = context.getCacheDir(); } else { // Check if the given directory exists or try to create it. - File file = new File(directory); - if (!file.isDirectory() && !file.mkdirs()) { + if (!directory.isDirectory() && !directory.mkdirs()) { return null; } } diff --git a/src/com/android/gallery3d/photoeditor/SaveCopyTask.java b/src/com/android/gallery3d/photoeditor/SaveCopyTask.java index b7d56261e..6b7b7c38f 100644 --- a/src/com/android/gallery3d/photoeditor/SaveCopyTask.java +++ b/src/com/android/gallery3d/photoeditor/SaveCopyTask.java @@ -48,29 +48,24 @@ public class SaveCopyTask extends AsyncTask<Bitmap, Void, Uri> { void onComplete(Uri result); } - private static final String TIME_STAMP_NAME = "'IMG'_yyyyMMdd_HHmmss"; - private static final int INDEX_DATE_TAKEN = 0; - private static final int INDEX_LATITUDE = 1; - private static final int INDEX_LONGITUDE = 2; + private interface ContentResolverQueryCallback { + + void onCursorResult(Cursor cursor); + } - private static final String[] IMAGE_PROJECTION = new String[] { - ImageColumns.DATE_TAKEN, - ImageColumns.LATITUDE, - ImageColumns.LONGITUDE, - }; + private static final String TIME_STAMP_NAME = "'IMG'_yyyyMMdd_HHmmss"; private final Context context; private final Uri sourceUri; private final Callback callback; - private final String albumName; private final String saveFileName; + private File saveDirectory; public SaveCopyTask(Context context, Uri sourceUri, Callback callback) { this.context = context; this.sourceUri = sourceUri; this.callback = callback; - albumName = context.getString(R.string.edited_photo_bucket_name); saveFileName = new SimpleDateFormat(TIME_STAMP_NAME).format( new Date(System.currentTimeMillis())); } @@ -85,7 +80,9 @@ public class SaveCopyTask extends AsyncTask<Bitmap, Void, Uri> { return null; } Bitmap bitmap = params[0]; - File file = save(bitmap); + getSaveDirectory(); + File file = new BitmapUtils(context).saveBitmap( + bitmap, saveDirectory, saveFileName, Bitmap.CompressFormat.JPEG); Uri uri = (file != null) ? insertContent(file) : null; bitmap.recycle(); return uri; @@ -94,7 +91,7 @@ public class SaveCopyTask extends AsyncTask<Bitmap, Void, Uri> { @Override protected void onPostExecute(Uri result) { String message = (result == null) ? context.getString(R.string.saving_failure) - : context.getString(R.string.photo_saved, albumName); + : context.getString(R.string.photo_saved, saveDirectory.getName()); Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); @@ -102,54 +99,76 @@ public class SaveCopyTask extends AsyncTask<Bitmap, Void, Uri> { callback.onComplete(result); } - private File save(Bitmap bitmap) { - String directory = Environment.getExternalStorageDirectory().toString() + "/" + albumName; - return new BitmapUtils(context).saveBitmap( - bitmap, directory, saveFileName, Bitmap.CompressFormat.JPEG); - } - - /** - * Insert the content (saved file) with proper source photo properties. - */ - private Uri insertContent(File file) { - long now = System.currentTimeMillis() / 1000; - long dateTaken = now; - double latitude = 0f; - double longitude = 0f; - + private void querySource(String[] projection, ContentResolverQueryCallback callback) { ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = null; try { - cursor = contentResolver.query(sourceUri, IMAGE_PROJECTION, null, null, null); + cursor = contentResolver.query(sourceUri, projection, null, null, null); if ((cursor != null) && cursor.moveToNext()) { - dateTaken = cursor.getLong(INDEX_DATE_TAKEN); - latitude = cursor.getDouble(INDEX_LATITUDE); - longitude = cursor.getDouble(INDEX_LONGITUDE); + callback.onCursorResult(cursor); } } catch (Exception e) { - // Ignore error for lacking property columns from the source. + // Ignore error for lacking the data column from the source. } finally { if (cursor != null) { cursor.close(); } } + } + + private void getSaveDirectory() { + querySource(new String[] { ImageColumns.DATA }, new ContentResolverQueryCallback () { - ContentValues values = new ContentValues(); + @Override + public void onCursorResult(Cursor cursor) { + saveDirectory = new File(cursor.getString(0)).getParentFile(); + } + }); + // Use the default save directory if the source directory cannot be saved. + if ((saveDirectory == null) || !saveDirectory.canWrite()) { + saveDirectory = new File(Environment.getExternalStorageDirectory(), + context.getString(R.string.edited_photo_bucket_name)); + } + } + + /** + * Insert the content (saved file) with proper source photo properties. + */ + private Uri insertContent(File file) { + long now = System.currentTimeMillis() / 1000; + + final ContentValues values = new ContentValues(); values.put(Images.Media.TITLE, saveFileName); values.put(Images.Media.DISPLAY_NAME, file.getName()); values.put(Images.Media.MIME_TYPE, "image/jpeg"); - values.put(Images.Media.DATE_TAKEN, dateTaken); + values.put(Images.Media.DATE_TAKEN, now); values.put(Images.Media.DATE_MODIFIED, now); values.put(Images.Media.DATE_ADDED, now); values.put(Images.Media.ORIENTATION, 0); values.put(Images.Media.DATA, file.getAbsolutePath()); values.put(Images.Media.SIZE, file.length()); - // TODO: Change || to && after the default location issue is fixed. - if ((latitude != 0f) || (longitude != 0f)) { - values.put(Images.Media.LATITUDE, latitude); - values.put(Images.Media.LONGITUDE, longitude); - } - return contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values); + String[] projection = new String[] { + ImageColumns.DATE_TAKEN, + ImageColumns.LATITUDE, + ImageColumns.LONGITUDE, + }; + querySource(projection, new ContentResolverQueryCallback() { + + @Override + public void onCursorResult(Cursor cursor) { + values.put(Images.Media.DATE_TAKEN, cursor.getLong(0)); + + double latitude = cursor.getDouble(1); + double longitude = cursor.getDouble(2); + // TODO: Change || to && after the default location issue is fixed. + if ((latitude != 0f) || (longitude != 0f)) { + values.put(Images.Media.LATITUDE, latitude); + values.put(Images.Media.LONGITUDE, longitude); + } + } + }); + + return context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values); } } |