private FilterShowActivity mActivity = null;
+ public static final String JPEG_MIME_TYPE = "image/jpeg";
+
public static final String DEFAULT_SAVE_DIRECTORY = "EditedOnlinePhotos";
public static final int DEFAULT_COMPRESS_QUALITY = 95;
public static int getOrientation(Context context, Uri uri) {
if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
- return getOrientationFromPath(uri.getPath());
+ String mimeType = context.getContentResolver().getType(uri);
+ if (mimeType != ImageLoader.JPEG_MIME_TYPE) {
+ return -1;
+ }
+ String path = uri.getPath();
+ int orientation = -1;
+ InputStream is = null;
+ try {
+ is = new FileInputStream(path);
+ ExifParser parser = ExifParser.parse(is, ExifParser.OPTION_IFD_0);
+ int event = parser.next();
+ while (event != ExifParser.EVENT_END) {
+ if (event == ExifParser.EVENT_NEW_TAG) {
+ ExifTag tag = parser.getTag();
+ if (tag.getTagId() == ExifTag.TAG_ORIENTATION) {
+ orientation = (int) tag.getValueAt(0);
+ break;
+ }
+ }
+ event = parser.next();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (ExifInvalidFormatException e) {
+ e.printStackTrace();
+ } finally {
+ Utils.closeSilently(is);
+ }
+ return orientation;
}
-
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri,
}
}
- static int getOrientationFromPath(String path) {
- int orientation = -1;
- InputStream is = null;
- try {
- is = new FileInputStream(path);
- ExifParser parser = ExifParser.parse(is, ExifParser.OPTION_IFD_0);
- int event = parser.next();
- while (event != ExifParser.EVENT_END) {
- if (event == ExifParser.EVENT_NEW_TAG) {
- ExifTag tag = parser.getTag();
- if (tag.getTagId() == ExifTag.TAG_ORIENTATION) {
- orientation = (int) tag.getValueAt(0);
- break;
- }
- }
- event = parser.next();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ExifInvalidFormatException e) {
- e.printStackTrace();
- } finally {
- Utils.closeSilently(is);
- }
- return orientation;
- }
-
private void updateBitmaps() {
if (mOrientation > 1) {
mOriginalBitmapSmall = rotateToPortrait(mOriginalBitmapSmall, mOrientation);
bitmap.getHeight(), matrix, true);
}
- private void closeStream(Closeable stream) {
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
private Bitmap loadRegionBitmap(Uri uri, Rect bounds) {
InputStream is = null;
try {
} catch (Exception e) {
e.printStackTrace();
} finally {
- closeStream(is);
+ Utils.closeSilently(is);
}
return null;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
- closeStream(is);
+ Utils.closeSilently(is);
is = mContext.getContentResolver().openInputStream(uri);
return BitmapFactory.decodeStream(is, null, o2);
} catch (FileNotFoundException e) {
} catch (Exception e) {
e.printStackTrace();
} finally {
- closeStream(is);
+ Utils.closeSilently(is);
}
return null;
}
}).execute(preset);
}
- public static Bitmap loadMutableBitmap(Context context, Uri sourceUri)
- throws FileNotFoundException {
+ public static Bitmap loadMutableBitmap(Context context, Uri sourceUri) {
BitmapFactory.Options options = new BitmapFactory.Options();
// TODO: on <3.x we need a copy of the bitmap (inMutable doesn't
// exist)
options.inMutable = true;
- InputStream is = context.getContentResolver().openInputStream(sourceUri);
- Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
+ InputStream is = null;
+ Bitmap bitmap = null;
+ try {
+ is = context.getContentResolver().openInputStream(sourceUri);
+ bitmap = BitmapFactory.decodeStream(is, null, options);
+ } catch (FileNotFoundException e) {
+ Log.w(LOGTAG, "could not load bitmap ", e);
+ is = null;
+ bitmap = null;
+ } finally {
+ Utils.closeSilently(is);
+ }
+ if (bitmap == null) {
+ return null;
+ }
int orientation = ImageLoader.getOrientation(context, sourceUri);
bitmap = ImageLoader.rotateToPortrait(bitmap, orientation);
return bitmap;
if (param == null) {
return null;
}
- try {
- Bitmap bitmap = param.apply(loadMutableBitmap(mContext, mUri));
- return bitmap;
- } catch (FileNotFoundException ex) {
- Log.w(LOGTAG, "Failed to save image!", ex);
+ Bitmap bitmap = loadMutableBitmap(mContext, mUri);
+ if (bitmap == null) {
+ Log.w(LOGTAG, "Failed to save image!");
return null;
}
+ return param.apply(bitmap);
}
};
* @return true if it is a light Cycle image that is full 360
*/
public boolean queryLightCycle360() {
+ InputStream is = null;
try {
- InputStream is = mContext.getContentResolver().openInputStream(getUri());
+ is = mContext.getContentResolver().openInputStream(getUri());
XMPMeta meta = XmpUtilHelper.extractXMPMeta(is);
if (meta == null) {
return false;
}
String name = meta.getPacketHeader();
- try {
- String namespace = "http://ns.google.com/photos/1.0/panorama/";
- String cropWidthName = "GPano:CroppedAreaImageWidthPixels";
- String fullWidthName = "GPano:FullPanoWidthPixels";
-
- if (!meta.doesPropertyExist(namespace, cropWidthName)) {
- return false;
- }
- if (!meta.doesPropertyExist(namespace, fullWidthName)) {
- return false;
- }
-
- Integer cropValue = meta.getPropertyInteger(namespace, cropWidthName);
- Integer fullValue = meta.getPropertyInteger(namespace, fullWidthName);
-
- // Definition of a 360:
- // GFullPanoWidthPixels == CroppedAreaImageWidthPixels
- if (cropValue != null && fullValue != null) {
- return cropValue.equals(fullValue);
- }
+ String namespace = "http://ns.google.com/photos/1.0/panorama/";
+ String cropWidthName = "GPano:CroppedAreaImageWidthPixels";
+ String fullWidthName = "GPano:FullPanoWidthPixels";
+ if (!meta.doesPropertyExist(namespace, cropWidthName)) {
return false;
- } catch (XMPException e) {
+ }
+ if (!meta.doesPropertyExist(namespace, fullWidthName)) {
return false;
}
+
+ Integer cropValue = meta.getPropertyInteger(namespace, cropWidthName);
+ Integer fullValue = meta.getPropertyInteger(namespace, fullWidthName);
+
+ // Definition of a 360:
+ // GFullPanoWidthPixels == CroppedAreaImageWidthPixels
+ if (cropValue != null && fullValue != null) {
+ return cropValue.equals(fullValue);
+ }
+
+ return false;
} catch (FileNotFoundException e) {
return false;
+ } catch (XMPException e) {
+ return false;
+ } finally {
+ Utils.closeSilently(is);
}
}
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.AsyncTask;
import com.android.gallery3d.filtershow.presets.ImagePreset;
import com.android.gallery3d.util.XmpUtilHelper;
-import java.io.Closeable;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
} catch (FileNotFoundException e) {
Log.v(LOGTAG,"Error in writing "+destination.getAbsolutePath());
} finally {
- closeStream(os);
+ Utils.closeSilently(os);;
}
if (xmp != null) {
XmpUtilHelper.writeXMPMeta(destination.getAbsolutePath(), xmp);
}
-
- }
-
- private static void closeStream(Closeable stream) {
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
}
/**
}
private ExifData getExifData(Uri sourceUri) {
- String path = null;
- if (ContentResolver.SCHEME_FILE.equals(sourceUri.getScheme())) {
- path = sourceUri.getPath();
- }
-
- final String[] PROJECTION = new String[] {
- ImageColumns.DATA
- };
- try {
- Cursor c = context.getContentResolver().query(sourceUri, PROJECTION,
- null, null, null);
- if (c.moveToFirst()) {
- path = c.getString(0);
- }
- c.close();
- } catch (Exception e) {
- Log.w(LOGTAG, "Failed to copy exif", e);
+ String mimeType = context.getContentResolver().getType(sourceUri);
+ if (mimeType != ImageLoader.JPEG_MIME_TYPE) {
+ return null;
}
-
InputStream is = null;
try {
- is = new FileInputStream(path);
+ is = context.getContentResolver().openInputStream(sourceUri);
ExifReader reader = new ExifReader();
return reader.read(is);
} catch (FileNotFoundException e) {
+ Log.w(LOGTAG, "Failed to find file", e);
return null;
} catch (ExifInvalidFormatException e) {
+ Log.w(LOGTAG, "Invalid EXIF data", e);
return null;
} catch (IOException e) {
Log.w(LOGTAG, "Failed to read original file", e);
} finally {
Utils.closeSilently(is);
}
-
}
/**
if (params[0] == null) {
return null;
}
-
ImagePreset preset = params[0];
-
+ InputStream is = null;
try {
- Bitmap bitmap = preset.apply(ImageLoader.loadMutableBitmap(context, sourceUri));
+ Bitmap bitmap = ImageLoader.loadMutableBitmap(context, sourceUri);
+ if (bitmap == null) {
+ return null;
+ }
+ bitmap = preset.apply(bitmap);
Object xmp = null;
- InputStream is = null;
if (preset.isPanoramaSafe()) {
is = context.getContentResolver().openInputStream(sourceUri);
xmp = XmpUtilHelper.extractXMPMeta(is);
}
ExifData exif = getExifData(sourceUri);
- exif.addDateTimeStampTag(ExifTag.TAG_DATE_TIME, System.currentTimeMillis(),
- TimeZone.getDefault());
- // Since the image has been modified, set the orientation to normal.
- exif.addTag(ExifTag.TAG_ORIENTATION).setValue(ExifTag.Orientation.TOP_LEFT);
+ if (exif != null) {
+ exif.addDateTimeStampTag(ExifTag.TAG_DATE_TIME, System.currentTimeMillis(),
+ TimeZone.getDefault());
+ // Since the image has been modified, set the orientation to normal.
+ exif.addTag(ExifTag.TAG_ORIENTATION).setValue(ExifTag.Orientation.TOP_LEFT);
+ }
saveBitmap(bitmap, this.destinationFile, xmp, exif);
Uri uri = insertContent(context, sourceUri, this.destinationFile, saveFileName);
bitmap.recycle();
return uri;
-
} catch (FileNotFoundException ex) {
Log.w(LOGTAG, "Failed to save image!", ex);
return null;
+ } finally {
+ Utils.closeSilently(is);
}
}