0

I am trying to get a big size image programmatically , but app is crashing. Here is what I'm doing:

public static Bitmap getImageFromResult(Context context, int resultCode, Intent imageReturnedIntent) {
    Bitmap bitmap = null;
    File imageFile = getTempFile(context);

    if (resultCode == Activity.RESULT_OK) {
        Uri selectedImage;
        boolean isCamera = (imageReturnedIntent == null || imageReturnedIntent.getData() == null || imageReturnedIntent.getData().toString().contains(imageFile.toString()));

        if (isCamera) {
            // From Camera
            selectedImage = Uri.fromFile(imageFile);
        } else {
            // From Storage
            selectedImage = imageReturnedIntent.getData();
        }

        //bitmap = getResizedImage(context, selectedImage);
        bitmap = getResizedImage(context, selectedImage);

    }
    return bitmap;
}

And the resizemethod` :

//*********** Resize to avoid using too much Memory loading Big Images (2560*1920) ********//

private static Bitmap getResizedImage(Context context, Uri selectedImage) {
    Bitmap resizedBitmap = null;
    int[] sampleSizes = new int[]{8,7,6,5, 4, 3, 2, 1};

    int i = 0;
    do {
        resizedBitmap = decodeBitmap(context, selectedImage, sampleSizes[i]);
        i++;

    } while (resizedBitmap.getWidth() < minWidthQuality && i < sampleSizes.length);

    return resizedBitmap;
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

Try this - you can resize the Bitmap to another height/width

 public static Bitmap getResizedBitmap(Bitmap bm, float newHeight, float newWidth) {
        float width = bm.getWidth();
        float height = bm.getHeight();
        float scaleWidth = (newWidth) / width;
        float scaleHeight = (newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(bm, 0, 0, (int) width, (int) height, matrix, false);
    }

Then just make your new Bitmap

 Bitmap bitmap = BitmapFactory.decodeFile(pathname);
 Bitmap newBitmap = getResizedBitmap(bitmap,400,320);
Beasteca
  • 83
  • 9
0

If you are getting image live from internet then obviously you need to first download the image but it will take time according to internet speed or you want to load image from memory then you can apply this stack over answer method here to get decrease the image quality

sourabh kaushik
  • 523
  • 4
  • 20