public static Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
private Bitmap decodeBitmapUri(Context ctx, Uri uri) throws FileNotFoundException {
int targetW = 600;
int targetH = 600;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(ctx.getContentResolver().openInputStream(uri), null, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
Bitmap bm = BitmapFactory.decodeStream(ctx.getContentResolver().openInputStream(uri), null, bmOptions);
int rotation = this.getWindowManager().getDefaultDisplay().getRotation();
if(rotation == 0){ //portrait
rotation = 90;
}
else if(rotation == 1){ //landscape
rotation = 0;
}
else if(rotation == 2){
rotation = 270;
}
else if(rotation == 3){
rotation = 180;
}
bm = rotate(bm, rotation);
return bm;
}