OSDN Git Service

Fix 2145588 jhead reports "Not JPEG"
authorRay Chen <raychen@google.com>
Fri, 25 Sep 2009 20:43:31 +0000 (13:43 -0700)
committerRay Chen <raychen@google.com>
Sun, 27 Sep 2009 18:47:56 +0000 (11:47 -0700)
src/com/android/camera/Camera.java
src/com/android/camera/CropImage.java
src/com/android/camera/ImageManager.java
src/com/android/camera/gallery/BaseImage.java
src/com/android/camera/gallery/Image.java

index b7f09bf..a8eb349 100644 (file)
@@ -544,16 +544,15 @@ public class Camera extends Activity implements View.OnClickListener,
                         loc, // location for the database goes here
                         0, // the dsp will use the right orientation so
                            // don't "double set it"
-                        ImageManager.CAMERA_IMAGE_BUCKET_NAME,
-                        name);
+                        ImageManager.CAMERA_IMAGE_BUCKET_NAME, name, null, data);
                 if (mLastContentUri == null) {
                     // this means we got an error
                     mCancel = true;
                 }
                 int degree = 0;
                 if (!mCancel) {
-                    degree = ImageManager.storeImage(mLastContentUri, mContentResolver,
-                            null, data);
+                    degree = ImageManager.getExifOrientation(
+                            ImageManager.CAMERA_IMAGE_BUCKET_NAME + "/" + name);
                     ImageManager.setImageSize(mContentResolver, mLastContentUri,
                             new File(ImageManager.CAMERA_IMAGE_BUCKET_NAME,
                             name).length());
index 88060a2..358675e 100644 (file)
@@ -374,14 +374,8 @@ public class CropImage extends MonitoredActivity {
                                  // the location (gps).
                         0,       // TODO this is going to cause the orientation
                                  // to reset.
-                        directory.toString(),
-                        fileName + "-" + x + ".jpg");
-
-                ImageManager.storeImage(
-                        newUri,
-                        mContentResolver,
-                        croppedImage,
-                        null);
+                        directory.toString(), fileName + "-" + x + ".jpg",
+                        croppedImage, null);
 
                 setResult(RESULT_OK, new Intent()
                         .setAction(newUri.toString())
index 4b9b0cf..f4a5e6f 100644 (file)
@@ -32,6 +32,7 @@ import android.content.ContentUris;
 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;
@@ -45,7 +46,10 @@ import android.provider.MediaStore.Images.ImageColumns;
 import android.util.Log;
 
 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.HashMap;
 import java.util.Iterator;
@@ -223,7 +227,28 @@ public class ImageManager {
 
     public static Uri addImage(ContentResolver cr, String title,
             long dateTaken, Location location,
-            int orientation, String directory, String filename) {
+            int orientation, String directory, String filename, Bitmap source, byte[] jpegData) {
+        // 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;
+        File file = new File(directory, filename);
+        try {
+            file.mkdirs();
+            outputStream = new FileOutputStream(file);
+            if (source != null) {
+                source.compress(CompressFormat.JPEG, 75, outputStream);
+            } else {
+                outputStream.write(jpegData);
+            }
+        } 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);
@@ -235,72 +260,16 @@ public class ImageManager {
         values.put(Images.Media.DATE_TAKEN, dateTaken);
         values.put(Images.Media.MIME_TYPE, "image/jpeg");
         values.put(Images.Media.ORIENTATION, orientation);
+        values.put(Images.Media.DATA, directory + "/" + filename);
 
         if (location != null) {
             values.put(Images.Media.LATITUDE, location.getLatitude());
             values.put(Images.Media.LONGITUDE, location.getLongitude());
         }
 
-        if (directory != null && filename != null) {
-            String value = directory + "/" + filename;
-            values.put(Images.Media.DATA, value);
-        }
-
         return cr.insert(STORAGE_URI, values);
     }
 
-    // Returns the rotation degree stored in the jpeg header.
-    public static int storeImage(Uri uri, ContentResolver cr,
-            Bitmap source, byte [] jpegData) {
-
-        if (source == null && jpegData == null || uri == null) {
-            throw new IllegalArgumentException("source cannot be null");
-        }
-
-        boolean complete = false;
-        try {
-            long id = ContentUris.parseId(uri);
-            BaseImageList il = new ImageList(
-                    cr, STORAGE_URI, THUMB_URI, SORT_ASCENDING, null);
-
-            // TODO: Redesign the process of adding new images. We should
-            //     create an <code>IImage</code> in "ImageManager.addImage"
-            //     and pass the image object to here.
-            Image image = new Image(il, cr, id, 0, il.contentUri(id), null,
-                    0, null, 0, null, null, 0);
-            String[] projection = new String[] {
-                    ImageColumns._ID, ImageColumns.DATA};
-            Cursor c = cr.query(uri, projection, null, null, null);
-            String filepath;
-            try {
-                c.moveToPosition(0);
-                filepath = c.getString(1);
-            } finally {
-                c.close();
-            }
-            image.saveImageContents(source, jpegData, true, filepath);
-
-            ContentValues values = new ContentValues();
-            values.put(ImageColumns.MINI_THUMB_MAGIC, 0);
-            int degree = 0;
-            if (jpegData != null) {
-                degree = getExifOrientation(filepath);
-            }
-            values.put(Images.Media.ORIENTATION, degree);
-            cr.update(uri, values, null, null);
-            complete = true;
-            return degree;
-        } finally {
-            if (!complete) {
-                try {
-                    cr.delete(uri, null, null);
-                } catch (Throwable t) {
-                    // ignore it while clean up.
-                }
-            }
-        }
-    }
-
     public static int getExifOrientation(String filepath) {
         int degree = 0;
         ExifInterface exif = null;
index aa7116d..5c9c07f 100644 (file)
@@ -85,16 +85,11 @@ public abstract class BaseImage implements IImage {
      * @param uri       where to store the bitmap
      * @return          true if we succeeded
      */
-    protected boolean compressImageToFile(
-            Bitmap bitmap, byte [] jpegData, Uri uri) {
+    protected boolean compressImageToFile(Bitmap bitmap, Uri uri) {
         OutputStream outputStream = null;
         try {
             outputStream = mContentResolver.openOutputStream(uri);
-            if (bitmap != null) {
-                bitmap.compress(compressionType(), 75, outputStream);
-            } else {
-                outputStream.write(jpegData);
-            }
+            bitmap.compress(compressionType(), 75, outputStream);
             return true;
         } catch (FileNotFoundException ex) {
             return false;
index 340ba79..d14c74e 100644 (file)
@@ -98,10 +98,9 @@ public class Image extends BaseImage implements IImage {
         mExif.setAttribute(tag, value);
     }
 
-    public void saveImageContents(Bitmap image, byte [] jpegData,
-            boolean newFile, String filePath) {
+    public void saveImageContents(Bitmap image, String filePath) {
         Uri uri = mContainer.contentUri(mId);
-        compressImageToFile(image, jpegData, uri);
+        compressImageToFile(image, uri);
     }
 
     private void loadExifData() {
@@ -178,4 +177,4 @@ public class Image extends BaseImage implements IImage {
 
         return bitmap;
     }
-}
\ No newline at end of file
+}