Loading image directly from gallary can kill the app .
we should never do that
we should first scale that image on our requirement and than should use that scaled image
some times app can not handle big bitmap in memmory
so below code will decode your image in required size just give sizeOfImage to your requred size
It will also manage your arspect ratio of image
Put below code in your onactivityresult
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap mm = decodeSampledBitmapFromResource(new File(filePath),sizeOfImage,sizeOfImage);
above code will use below function so also put below function at somewhere
public static Bitmap decodeSampledBitmapFromResource(File res, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(res.getAbsolutePath(),options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(res.getAbsolutePath(),options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height;
final int halfWidth = width;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
You should change this function as per your requirement
and also refer following link
https://developer.android.com/training/displaying-bitmaps/load-bitmap.html