You need to resize your image size before set into imageview (if you having very large image then you need to resize your image in thread).
So you need to call createFile(this,mUri)
and it will return you bitmap. I already put height and width hardcoded for now so you can change yourself.
/**
* Loads a bitmap and avoids using too much memory loading big images (e.g.: 2560*1920)
*/
private static Bitmap createFile(Context context, Uri theUri) {
Bitmap outputBitmap = null;
AssetFileDescriptor fileDescriptor;
try {
fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r");
BitmapFactory.Options options = new BitmapFactory.Options();
outputBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
options.inJustDecodeBounds = true;
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;
float maxHeight = 740.0f;
float maxWidth = 1280.0f;
float imgRatio = actualWidth / actualHeight;
float maxRatio = maxWidth / maxHeight;
if (actualHeight > maxHeight || actualWidth > maxWidth) {
if (imgRatio < maxRatio) {
imgRatio = maxHeight / actualHeight;
actualWidth = (int) (imgRatio * actualWidth);
actualHeight = (int) maxHeight;
} else if (imgRatio > maxRatio) {
imgRatio = maxWidth / actualWidth;
actualHeight = (int) (imgRatio * actualHeight);
actualWidth = (int) maxWidth;
} else {
actualHeight = (int) maxHeight;
actualWidth = (int) maxWidth;
}
}
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[16 * 1024];
outputBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
if (outputBitmap != null) {
Log.d(TAG, "Loaded image with sample size " + options.inSampleSize + "\t\t"
+ "Bitmap width: " + outputBitmap.getWidth()
+ "\theight: " + outputBitmap.getHeight());
}
fileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
return outputBitmap;
}
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
final float totalPixels = width * height;
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
return inSampleSize;
}