Cooper Scott
Cooper Scott

Reputation: 706

Android Bitmap Downscaling Not working

I am new to bitmaps, downscaling, and Java in general, so please explain things in a little more depth than you normally would. Thank you. lol

My Problem: I am trying to downscale images to remove that nasty "Java.lang.OutOfMemory" Error! When I attempt to downscale, when I launch the app, instead of being the image there, there is no image there.

Maybe you can help :)

My two Methods:

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 / 2;
        final int halfWidth = width / 2;

        // 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;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

and my onCreate Method (I'm sure it is some really stupid mistake, so please, go easy on me(lol sorry)):

ImageView image9View;
ImageView image8View;
ImageView image7View;
ImageView image6View;
ImageView image5View;
ImageView image4View;
ImageView image3View;
ImageView image2View;
ImageView image1View;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




    image9View = (ImageView)findViewById(R.id.faqImage);
    image8View = (ImageView)findViewById(R.id.foodIcon);
    image7View = (ImageView)findViewById(R.id.bracketsIcon);
    image6View = (ImageView)findViewById(R.id.teamsIcon);
    image5View = (ImageView)findViewById(R.id.playersIcon);
    image4View = (ImageView)findViewById(R.id.gamesIcon);
    image3View = (ImageView)findViewById(R.id.homeIcon);
    image2View = (ImageView)findViewById(R.id.settingsIcon);
    image1View = (ImageView)findViewById(R.id.alert);


    image9View.setImageBitmap(
            decodeSampledBitmapFromResource(getResources(), R.id.faqImage, 50, 50));
    image8View.setImageBitmap(
            decodeSampledBitmapFromResource(getResources(), R.id.foodIcon, 50, 50));
    image7View.setImageBitmap(
            decodeSampledBitmapFromResource(getResources(), R.id.bracketsIcon, 50, 50));
    image6View.setImageBitmap(
            decodeSampledBitmapFromResource(getResources(), R.id.teamsIcon, 50, 50));
    image5View.setImageBitmap(
            decodeSampledBitmapFromResource(getResources(), R.id.playersIcon, 50, 50));
    image4View.setImageBitmap(
            decodeSampledBitmapFromResource(getResources(), R.id.gamesIcon, 50, 50));
    image3View.setImageBitmap(
            decodeSampledBitmapFromResource(getResources(), R.id.homeIcon, 50, 50));
    image2View.setImageBitmap(
            decodeSampledBitmapFromResource(getResources(), R.id.settingsIcon, 50, 50));
    image1View.setImageBitmap(
            decodeSampledBitmapFromResource(getResources(), R.id.alert, 50, 50));}

The problem? I have no idea. I'm new to this stuff, so I am asking you. Thank you :)

Upvotes: 3

Views: 230

Answers (1)

Gil Vegliach
Gil Vegliach

Reputation: 3562

The code is almost alright, you mixed up 'view ids' (R.id.stuff_i_defined_in_xml) and 'resource ids' (e.g. R.drawable.my_png_file). Try with:

image9View.setImageBitmap(
        decodeSampledBitmapFromResource(getResources(), R.drawable.faqImage, 50, 50));

Make sure you have a file called "faqImage.png" in the drawables folders (one of res/drawable*).

Upvotes: 1

Related Questions