Source:
<code>private Bitmap getBitmap() { Bitmap bitmap = null; try { InputStream is = getActivity().getContentResolver().openInputStream(Uri.parse(photo)); Display display = getActivity().getWindowManager().getDefaultDisplay(); int targetWidth = display.getWidth(); int targetHeight = (int) getResources().getDimension(R.dimen.faciliteis_image_view); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getActivity().getContentResolver().query(Uri.parse(photo), filePathColumn, null, null, null); String imagePath; if (cursor == null) { imagePath = Uri.parse(photo).getPath(); } else { cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); imagePath = cursor.getString(columnIndex); } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(imagePath, options); int photoWidth = options.outWidth; int photoHeight = options.outHeight; <START> int scaleFactor = 1; <END> while (true) { if(photoWidth / 2 < targetWidth && photoHeight / 2 < targetHeight) { break; } photoWidth /= 2; photoHeight /= 2; scaleFactor++; } options.inJustDecodeBounds = false; options.inSampleSize = scaleFactor; bitmap = BitmapFactory.decodeStream(is, null, options); ExifInterface ei = new ExifInterface(imagePath); int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); bitmap = rotateImage(bitmap, orientation); } catch (FileNotFoundException e1) { LoggingManager.Timber.d("____________photo error____________", e1); } catch (IOException e2) { LoggingManager.Timber.d("____________photo error____________", e2); } return bitmap; }</code><technical_language>A cleaner implementation this (since scaleFactor a power of 2): while(photoWidth / scaleFactor > targetWidth || photoHeight / scaleFactor > targetHeight) { scaleFactor *=2; }</technical_language>

Target:
private Bitmap getBitmap() { Bitmap bitmap = null; try { InputStream is = getActivity().getContentResolver().openInputStream(Uri.parse(photo)); Display display = getActivity().getWindowManager().getDefaultDisplay(); int targetWidth = display.getWidth(); int targetHeight = (int) getResources().getDimension(R.dimen.faciliteis_image_view); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getActivity().getContentResolver().query(Uri.parse(photo), filePathColumn, null, null, null); String imagePath; if (cursor == null) { imagePath = Uri.parse(photo).getPath(); } else { cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); imagePath = cursor.getString(columnIndex); } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(imagePath, options); int photoWidth = options.outWidth; int photoHeight = options.outHeight; int scaleFactor = 1; while(photoWidth / scaleFactor > targetWidth || photoHeight / scaleFactor > targetHeight) { scaleFactor *= 2; } options.inJustDecodeBounds = false; options.inSampleSize = scaleFactor; bitmap = BitmapFactory.decodeStream(is, null, options); ExifInterface ei = new ExifInterface(imagePath); int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); bitmap = rotateImage(bitmap, orientation); } catch (FileNotFoundException e1) { LoggingManager.Timber.d("____________photo error____________", e1); } catch (IOException e2) { LoggingManager.Timber.d("____________photo error____________", e2); } return bitmap; }