0

I have a problem with a function that I implemented. On some phones I get a out of memory error.

private Bitmap getIconMarkerOfPlayer(Player p) {
    Drawable drawable = getIconOfPlayer(p);
    Bitmap img = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(img);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);

    final float scale = this.getResources().getDisplayMetrics().density;
    int heigthPixels = (int) (24 * scale + 0.5f);
    int widthPixels = (int) (24 * scale + 0.5f);
    return Bitmap.createScaledBitmap(img, widthPixels, heigthPixels, false);
}

The getIconMarkerOfPlayer(Player p) function gives a "programmed" drawable depending on the player's status (dead, alive, offline, ...) and its color. Each player has a unique color assigned to it in the early games.

How can I resize my bitmap from a Drawable object without having a out of memory error?

Parker
  • 7,244
  • 12
  • 70
  • 92
Quentin
  • 80
  • 9
  • try here [Displaying Bitmaps Efficiently](http://developer.android.com/training/displaying-bitmaps/index.html) if you haven't done so – Vitor Mota Nov 02 '15 at 21:39

1 Answers1

1

I suggest you to use some ImageLibraries to load Bitmaps efficiently.

Some of them are Fresco, Glide, Piccasio.

I suggest you to go with Glide. Have a look at it here

sha
  • 1,410
  • 2
  • 18
  • 37
  • The picture is not loaded from the internet. As made, the images exist in the drawable folder. `getIconMarkerOfPlayer (Player p)` takes the right drawable according to the player's condition and colorized with the color of the player. This new image is returned as a new drawable. The function that is the problem take this drawable and turns it into bitmap resized. It is used to display the correct icon of the player on a google map. – Quentin Nov 02 '15 at 23:03
  • You can still use Glide for scaling using Trasformations. If you only want to go with the native SDK, have a look at this http://stackoverflow.com/a/10703256/726625. This may help. – sha Nov 02 '15 at 23:12