Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.ExifInterface; import java.io.IOException; public class Main { public static Bitmap decodeSampledBitmapFromFd(String pathName, int reqWidth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(pathName, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; Bitmap src = BitmapFactory.decodeFile(pathName, options); int degree = readPictureDegree(pathName); return rotateBitmap(src, degree); } private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { int sampleSize = 1; int height = options.outHeight; int width = options.outWidth; if (height > reqHeight || width > reqWidth) { final int heightRatio; final int widthRatio; if (reqHeight == 0) { sampleSize = (int) Math.floor((float) width / (float) reqWidth); } else if (reqWidth == 0) { sampleSize = (int) Math.floor((float) height / (float) reqHeight); } else { heightRatio = (int) Math.floor((float) height / (float) reqHeight); widthRatio = (int) Math.floor((float) width / (float) reqWidth); sampleSize = Math.max(heightRatio, widthRatio); } } return sampleSize; } public static int readPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; } public static Bitmap rotateBitmap(Bitmap source, int angle) { Matrix matrix = new Matrix(); if (angle > 0) matrix.setRotate(angle); Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); if (bitmap != source && !source.isRecycled()) source.recycle(); return bitmap; } }