Bitmap Downscaling does not work

I'm new to bitmaps, downscaling and Java in general, so please explain things a little deeper than usual. Thank. lol

My problem: I am trying to zoom out to remove this nasty "Java.lang.OutOfMemory" error! When I try to zoom out, when I run the application, there is no image there instead of an image.

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 this is some really stupid mistake, so please understand me calmly (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));}

      

Problem? I have no idea. I'm new to this, so I ask you. Thank:)

+3


source to share


1 answer


The code is almost fine, you confused the "view id" (R.id.stuff_i_defined_in_xml) and the "resource IDs" (eg R.drawable.my_png_file). Try:

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

      



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

+1


source







All Articles