Java – Android – Drawing images from Assets, something strange happened

Android – Drawing images from Assets, something strange happened… here is a solution to the problem.

Android – Drawing images from Assets, something strange happened

I’m loading an image in the Assets folder to draw as an overlay on Google Maps View. In my try-catch statement, I try to load an image, otherwise it returns null.

What I don’t understand is that after executing the code again and again, it reaches the loading line and fails, returning null, but for some reason the image is actually loaded and drawn correctly despite returning an empty image…?

If I run the program normally, it loads the image and displays it correctly, but the function still returns an empty bitmap every time. Am I missing something here?

Here is the code I used to load the image :

BitmapFactory.decodeStream(myActivity.getAssets().open("squirrel.jpg"));

Edit:
I tried another random image, this time .png, which still does the same thing but still loads and draws correctly.

Solution

Your debugger might jump to the return null line in a catch statement because the debugger likes to make you think it’s going back on a line that isn’t.

Related Problems and Solutions