1

When testing out my app on a phone, I got an out of memory error, and after some research, I found that it's due to my inappropriate usage of Bitmaps. Initially, I would declare a bitmap as such:

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image_name);

From the log, I saw that the error stems from that. So, I read around and tried using an extra parameter, options, as such:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

Then declaring the bitmap...

image = BitmapFactory.decodeResource(getResources(), R.drawable.image_name, options);

When running this, I don't get the out of memory error anymore, but I do receive a null pointer exception when trying to use the bitmaps, if its trying to draw them to my canvas, or calling .getWidth() and .getHeight() on them.

I'm having a hard time trying to fix this and some feedback would be much appreciated.

Error Log:

01-27 18:47:42.597: E/AndroidRuntime(15698): java.lang.NullPointerException
01-27 18:47:42.597: E/AndroidRuntime(15698):    at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1083)
01-27 18:47:42.597: E/AndroidRuntime(15698):    at android.graphics.Canvas.drawBitmap(Canvas.java:1139)
01-27 18:47:42.597: E/AndroidRuntime(15698):    at com.mascal.petele.Game$OurView.drawMenu(Game.java:654)
01-27 18:47:42.597: E/AndroidRuntime(15698):    at com.mascal.petele.Looping.run(Looping.java:34)

At Game.java:654 the line of code is:

c.drawBitmap(background, 0, 0, null);

This is the first occurrence of the usage of a bitmap, and happens for each other usage as well.

  • Debugger never lie. Please check the line that produces the error. Make sure that `image` is not `recycled` and `canvas` is initialized. More codes or log are preferable for better answer. – T D Nguyen Jan 27 '15 at 23:42
  • Are you checking the value returned by `BitmapFactory.decodeResource`? Also keep in mind processes in Android have limited memory available, so don't use uncompressed or too big images – m0skit0 Jan 27 '15 at 23:52
  • So, what's background? Seems like that is your null pointer. – iheanyi Jan 27 '15 at 23:55
  • Background is the name of the bitmap. I just checked the value of the Bitmap that causes the nullpointerexception and it is in fact null. Also the images themselves arent too big, theyre at most 16kb. I had thought that use decodeResource would solve the issue of using up too much memory, but I guess I'm still not using it correctly. – user2387830 Jan 27 '15 at 23:56
  • 1
    This is what inJustDecodeBounds means: If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels. So that guarantees a null bitmap. – iheanyi Jan 28 '15 at 00:00

2 Answers2

0

Here is the doc for inJustDecodeBounds: http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inJustDecodeBounds

Here is the text, highlighting my own:

If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

As you can see, you set an option that guarantees you will get a null bitmap!

iheanyi
  • 3,107
  • 2
  • 23
  • 22
  • Then I guess the initial problem remains as my attempted solution to the outofmemory error doesn't work. – user2387830 Jan 28 '15 at 00:11
  • @user2387830 You should read this: http://stackoverflow.com/questions/16679138/android-bitmap-outofmemoryerror Also, accept my answer if you feel it adequately addresses the issue in this particular question. – iheanyi Jan 28 '15 at 00:14
0

Your background variable is set to null. Perhaps it is failing to load. Fix that first, so it points to a Bitmap object, then you will be able to draw it.

gknicker
  • 5,509
  • 2
  • 25
  • 41