Java tutorial
//package com.java2s; //License from project: Open Source License import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.net.Uri; import android.os.ParcelFileDescriptor; import java.io.Closeable; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; public class Main { static Bitmap decodeBitmap(Context context, Uri uri, BitmapFactory.Options options, int maxW, int maxH, int orientation, int pass) { Bitmap bitmap = null; Bitmap newBitmap = null; if (pass > 20) { return null; } InputStream stream = openInputStream(context, uri); if (null == stream) return null; try { // decode the bitmap via android BitmapFactory bitmap = BitmapFactory.decodeStream(stream, null, options); closeSilently(stream); if (bitmap != null) { newBitmap = resizeBitmap(bitmap, maxW, maxH, orientation); if (bitmap != newBitmap) { bitmap.recycle(); } bitmap = newBitmap; } } catch (OutOfMemoryError error) { closeSilently(stream); if (null != bitmap) { bitmap.recycle(); } options.inSampleSize += 1; bitmap = decodeBitmap(context, uri, options, maxW, maxH, orientation, pass + 1); } return bitmap; } /** * Return an {@link InputStream} from the given uri. ( can be a local * content, a file path or an http url ) * * @param context * @param uri * @return the {@link InputStream} from the given uri, null if uri cannot be * opened */ public static InputStream openInputStream(Context context, Uri uri) { if (null == uri) return null; final String scheme = uri.getScheme(); InputStream stream = null; if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) { // from file stream = openFileInputStream(uri.getPath()); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { // from content stream = openContentInputStream(context, uri); } return stream; } public static void closeSilently(final Closeable c) { if (c == null) return; try { c.close(); } catch (final Throwable t) { } } public static void closeSilently(final ParcelFileDescriptor c) { if (c == null) return; try { c.close(); } catch (final Throwable t) { } } public static void closeSilently(Cursor cursor) { if (cursor == null) return; try { if (cursor != null) cursor.close(); } catch (Throwable t) { } } public static Bitmap resizeBitmap(final Bitmap input, int destWidth, int destHeight) throws OutOfMemoryError { return resizeBitmap(input, destWidth, destHeight, 0); } /** * Resize a bitmap object to fit the passed width and height * * @param input * The bitmap to be resized * @param destWidth * Desired maximum width of the result bitmap * @param destHeight * Desired maximum height of the result bitmap * @return A new resized bitmap * @throws OutOfMemoryError * if the operation exceeds the available vm memory */ public static Bitmap resizeBitmap(final Bitmap input, int destWidth, int destHeight, int rotation) throws OutOfMemoryError { int dstWidth = destWidth; int dstHeight = destHeight; final int srcWidth = input.getWidth(); final int srcHeight = input.getHeight(); if (rotation == 90 || rotation == 270) { dstWidth = destHeight; dstHeight = destWidth; } boolean needsResize = false; float p; if ((srcWidth > dstWidth) || (srcHeight > dstHeight)) { needsResize = true; if ((srcWidth > srcHeight) && (srcWidth > dstWidth)) { p = (float) dstWidth / (float) srcWidth; dstHeight = (int) (srcHeight * p); } else { p = (float) dstHeight / (float) srcHeight; dstWidth = (int) (srcWidth * p); } } else { dstWidth = srcWidth; dstHeight = srcHeight; } if (needsResize || rotation != 0) { Bitmap output; if (rotation == 0) { output = Bitmap.createScaledBitmap(input, dstWidth, dstHeight, true); } else { Matrix matrix = new Matrix(); matrix.postScale((float) dstWidth / srcWidth, (float) dstHeight / srcHeight); matrix.postRotate(rotation); output = Bitmap.createBitmap(input, 0, 0, srcWidth, srcHeight, matrix, true); } return output; } else return input; } /** * Return a {@link FileInputStream} from the given path or null if file not * found * * @param path * the file path * @return the {@link FileInputStream} of the given path, null if * {@link FileNotFoundException} is thrown */ static InputStream openFileInputStream(String path) { try { return new FileInputStream(path); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } /** * Return a {@link BufferedInputStream} from the given uri or null if an * exception is thrown * * @param context * @param uri * @return the {@link InputStream} of the given path. null if file is not * found */ static InputStream openContentInputStream(Context context, Uri uri) { try { return context.getContentResolver().openInputStream(uri); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } }