0

I was looking at this answer over here, however it didn't really fix my problem.

For some reason the manifest.xml doesn't overwrite my changes, even though I deleted my build folder.

I am trying to load up 5 images on the screen, however even after optimizing them; they still run out of memory.

I was watching a video that teaches image scaling. It did solved a small portion of my problem. I am able to load my background image, however I wasn't able to load my other 4 images.

How can I optimize more efficiently? I was watching this video regards Bitmap optimization by Android, however I didn't understand much from it.

Here is my source code

MainActivity.java

package com.example.cliente.myapplication;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private static final float BYTES_PER_PX = 4.0f;
    ImageView backgroundImage, upImage, downImage, leftImage, alertImage;

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

        backgroundImage = findViewById(R.id.background_image);
        loadImage(backgroundImage, R.drawable.background);

        upImage = findViewById(R.id.up_arrow_image);
        loadImage(upImage, R.drawable.up);

        downImage = findViewById(R.id.down_arrow_image);
        loadImage(downImage, R.drawable.down);

        leftImage = findViewById(R.id.left_arrow_image);
        loadImage(leftImage, R.drawable.left);

        alertImage = findViewById(R.id.alert_image);
        loadImage(alertImage, R.drawable.alert);

    }

    private void loadImage(ImageView image, int imgSrc) {
        if (readBitmapInfo() > MemUtils.megabytesFree()) {
            subSampleImage(image, 32);
        } else {
            image.setImageResource(imgSrc);
        }
    }

    private float readBitmapInfo() {
        final Resources res = this.getResources();
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, R.drawable.background, options);
        final float imageHeight = options.outHeight;
        final float imageWidth = options.outWidth;

        return imageWidth * imageHeight * BYTES_PER_PX / MemUtils.BYTES_IN_MB;
    }

    private void subSampleImage(ImageView image, int powerOf2) {
        if (powerOf2 < 1 || powerOf2 > 2) {
            return;
        }

        final Resources res = this.getResources();
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inSampleSize = powerOf2;
        final Bitmap map = BitmapFactory.decodeResource(res, R.drawable.background, options);
        image.setImageBitmap(map);
    }

}

MemUtils.java

package com.example.cliente.myapplication;

public class MemUtils {
    public static final float BYTES_IN_MB = 1024.0f * 1024.0f;

    public static float megabytesFree() {
        final Runtime rt = Runtime.getRuntime();
        final float bytesUsed = rt.totalMemory();
        final float mbUsed = bytesUsed / BYTES_IN_MB;
        final float mbFree = megabytesAvailable() - mbUsed;
        return mbFree;
    }

    public static float megabytesAvailable() {
        final Runtime rt = Runtime.getRuntime();
        final float bytesAvailable = rt.maxMemory();
        return bytesAvailable / BYTES_IN_MB;
    }

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
IntFooBar
  • 174
  • 3
  • 18

1 Answers1

0

So I had 2 errors to begin with.

  • OOM (out of memory) exception, thanks too @Chisko, I used a library called Picasso to solve my problem.
  • Another issue was Canvas: trying to draw too large... problem. Which I needed to move my images from drawable-mdpi to drawable-xxhdpi.

Here is the solution to my problem:

package com.example.cliente.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

    ImageView backgroundImage, upImage, downImage, leftImage, alertImage;

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

        backgroundImage = findViewById(R.id.background_image);
        Picasso.get().load(R.drawable.background).into(backgroundImage);

        upImage = findViewById(R.id.up_arrow_image);
        Picasso.get().load(R.drawable.up).into(upImage);

        downImage = findViewById(R.id.down_arrow_image);
        Picasso.get().load(R.drawable.down).into(downImage);

        leftImage = findViewById(R.id.left_arrow_image);
        Picasso.get().load(R.drawable.left).into(leftImage);

        alertImage = findViewById(R.id.alert_image);
        Picasso.get().load(R.drawable.alert).into(alertImage);
    }
}
IntFooBar
  • 174
  • 3
  • 18