FileDocCategorySizeDatePackage
ImageUtils.javaAPI DocAndroid 5.1 API10650Thu Mar 12 22:22:52 GMT 2015com.android.ex.photo.util

ImageUtils

public class ImageUtils extends Object
Image utilities

Fields Summary
private static final String
TAG
private static final long
MIN_NORMAL_CLASS
Minimum class memory class to use full-res photos
private static final long
MIN_SMALL_CLASS
Minimum 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
Constructors Summary
Methods Summary
private static com.android.ex.photo.util.ImageUtils$InputStreamFactorycreateInputStreamFactory(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.BitmapResultcreateLocalBitmap(android.content.ContentResolver resolver, android.net.Uri uri, int maxSize)
Create a bitmap from a local URI

param
resolver The ContentResolver
param
uri The local URI
param
maxSize The maximum size (either width or height)
return
The new bitmap or null

        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.BitmapdecodeStream(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}.

param
factory Used to create input streams that holds the raw data to be decoded into a bitmap.
param
outPadding If not null, return the padding rect for the bitmap if it exists, otherwise set padding to [-1,-1,-1,-1]. If no bitmap is returned (null) then padding is unchanged.
param
opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.
return
The decoded bitmap, or null if the image data could not be decoded, or, if opts is non-null, if opts requested only the size be returned (in opts.outWidth and opts.outHeight)

        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.PointgetImageBounds(com.android.ex.photo.util.ImageUtils$InputStreamFactory factory)
Gets the image bounds

param
factory Used to create the InputStream.
return
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 booleanisImageMimeType(java.lang.String mimeType)

return
true if the MimeType type is image

     
        // 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/");