0

I'm trying to load a picture from the gallery into a bitmap using this code:

Bitmap myBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(adress), (int)viewWidth/2, (int)viewHeight/2, false);

where 'adress' is the adress of the picture in the gallery. This works fine on my Galaxy Note 1, android version 2.3.6, but crashes due to 'out of memory' on my Galaxy 2, android version 4.1.2

Am I doing something wrong here? Is there a way to get a scaled bitmap? The resulting bitmap (when this does work) is a bit smudged due to scaling, but I don't mind. Thanks!!!

2

2 Answers 2

2

use this method

    Bitmap bm=decodeSampledBitmapFromPath(src, reqWidth, reqHeight);

Implementation-

     public 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) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
        int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeFile(path, options);
    return bmp;
}

}

Sign up to request clarification or add additional context in comments.

1 Comment

this gives me the bitmaps in their original scale. I need the images to fill the necessary space, even though it means getting stretched
0

this is directly your answer - you need to calculate sizes first and then fetch the image in the correct size

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.