Android Studio Java Bitmap Factory returns null

Viewed 133

I'm trying to load .png file from resources, following code prints "img is null!" so BitmapFactory returns null, but Android Studio IDE doesn't show any errors.

API 16

img = BitmapFactory.decodeResource(context.getResources(), R.drawable.triangle_435);
if(img == null) {
  System.out.println("img is null!");
}

Res folder

1 Answers

The image is available for only API 24 and above(as indicated by v24).

Move your image to drawable folder to use the default drawable folder which provides the resources for all android versions and sizes.

To view the complete hierarchy, click on the project view instead of android

enter image description here

in the project navigation section(on left of code editor) and do the following. You can also use the refactor option on the image to move it to a different folder or try copy and paste, Android studio will provide you the options to paste in different drawable directories.

Read more about Providing alternative resources and read about Platform Version (API level) in the table.

Related