ImageUtilspublic class ImageUtils extends Object
Fields Summary |
---|
private static final String | TAG | private static final long | MIN_NORMAL_CLASSMinimum class memory class to use full-res photos | private static final long | MIN_SMALL_CLASSMinimum class memory class to use small photos | private static final String | BASE64_URI_PREFIX | private static final Pattern | BASE64_IMAGE_URI_PATTERN | public static final ImageSize | sUseImageSize |
Methods Summary |
---|
private static com.android.ex.photo.util.ImageUtils$InputStreamFactory | createInputStreamFactory(android.content.ContentResolver resolver, android.net.Uri uri)
final String scheme = uri.getScheme();
if ("data".equals(scheme)) {
return new DataInputStreamFactory(resolver, uri);
}
return new BaseInputStreamFactory(resolver, uri);
| public static com.android.ex.photo.loaders.PhotoBitmapLoaderInterface.BitmapResult | createLocalBitmap(android.content.ContentResolver resolver, android.net.Uri uri, int maxSize)Create a bitmap from a local URI
final BitmapResult result = new BitmapResult();
final InputStreamFactory factory = createInputStreamFactory(resolver, uri);
try {
final Point bounds = getImageBounds(factory);
if (bounds == null) {
result.status = BitmapResult.STATUS_EXCEPTION;
return result;
}
final BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = Math.max(bounds.x / maxSize, bounds.y / maxSize);
result.bitmap = decodeStream(factory, null, opts);
result.status = BitmapResult.STATUS_SUCCESS;
return result;
} catch (FileNotFoundException exception) {
// Do nothing - the photo will appear to be missing
} catch (IOException exception) {
result.status = BitmapResult.STATUS_EXCEPTION;
} catch (IllegalArgumentException exception) {
// Do nothing - the photo will appear to be missing
} catch (SecurityException exception) {
result.status = BitmapResult.STATUS_EXCEPTION;
}
return result;
| public static android.graphics.Bitmap | decodeStream(com.android.ex.photo.util.ImageUtils$InputStreamFactory factory, android.graphics.Rect outPadding, BitmapFactory.Options opts)Wrapper around {@link BitmapFactory#decodeStream(InputStream, Rect,
BitmapFactory.Options)} that returns {@code null} on {@link
OutOfMemoryError}.
InputStream is = null;
try {
// Determine the orientation for this image
is = factory.createInputStream();
final int orientation = Exif.getOrientation(is, -1);
if (is != null) {
is.close();
}
// Decode the bitmap
is = factory.createInputStream();
final Bitmap originalBitmap = BitmapFactory.decodeStream(is, outPadding, opts);
if (is != null && originalBitmap == null && !opts.inJustDecodeBounds) {
Log.w(TAG, "ImageUtils#decodeStream(InputStream, Rect, Options): "
+ "Image bytes cannot be decoded into a Bitmap");
throw new UnsupportedOperationException(
"Image bytes cannot be decoded into a Bitmap.");
}
// Rotate the Bitmap based on the orientation
if (originalBitmap != null && orientation != 0) {
final Matrix matrix = new Matrix();
matrix.postRotate(orientation);
return Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
originalBitmap.getHeight(), matrix, true);
}
return originalBitmap;
} catch (OutOfMemoryError oome) {
Log.e(TAG, "ImageUtils#decodeStream(InputStream, Rect, Options) threw an OOME", oome);
return null;
} catch (IOException ioe) {
Log.e(TAG, "ImageUtils#decodeStream(InputStream, Rect, Options) threw an IOE", ioe);
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// Do nothing
}
}
}
| private static android.graphics.Point | getImageBounds(com.android.ex.photo.util.ImageUtils$InputStreamFactory factory)Gets the image bounds
final BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
decodeStream(factory, null, opts);
return new Point(opts.outWidth, opts.outHeight);
| public static boolean | isImageMimeType(java.lang.String mimeType)
// On HC and beyond, assume devices are more capable
if (Build.VERSION.SDK_INT >= 11) {
sUseImageSize = ImageSize.NORMAL;
} else {
if (PhotoViewController.sMemoryClass >= MIN_NORMAL_CLASS) {
// We have plenty of memory; use full sized photos
sUseImageSize = ImageSize.NORMAL;
} else if (PhotoViewController.sMemoryClass >= MIN_SMALL_CLASS) {
// We have slight less memory; use smaller sized photos
sUseImageSize = ImageSize.SMALL;
} else {
// We have little memory; use very small sized photos
sUseImageSize = ImageSize.EXTRA_SMALL;
}
}
return mimeType != null && mimeType.startsWith("image/");
|
|