Android examples for android.graphics:Bitmap Load Save
Read Bitmap from file and rotate
import java.io.IOException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.ExifInterface; public class Main{ static public Bitmap getPic(String fileName) { /* There isn't enough memory to open up more than a couple camera photos */ /* So pre-scale the target bitmap into which the file is decoded */ /* Get the size of the ImageView */ int targetW = 278; int targetH = 200; int degree = getRotateDegreeFromExif(fileName); Matrix matrix = new Matrix(); matrix.postRotate(degree);/*from w w w. j a v a2 s .co m*/ /* Get the size of the image */ BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(fileName, bmOptions); int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; /* Figure out which way needs to be reduced less */ int scaleFactor = 1; if ((targetW > 0) || (targetH > 0)) { scaleFactor = Math.min(photoW / targetW, photoH / targetH); } /* Set bitmap options to scale the image decode target */ bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; Bitmap bitmap = BitmapFactory.decodeFile(fileName, bmOptions); if (bitmap == null) return null; Bitmap rotatedImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bitmap = null; return rotatedImage; } static private int getRotateDegreeFromExif(String filePath) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(filePath); int orientation = exifInterface.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { degree = 90; } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { degree = 180; } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { degree = 270; } if (degree != 0) { exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, "0"); exifInterface.saveAttributes(); } } catch (IOException e) { degree = -1; e.printStackTrace(); } return degree; } }