Path for getResourceAsStream in Android Project

2cupsOfTech picture 2cupsOfTech · Aug 30, 2015 · Viewed 7k times · Source

I am trying to programmatically load a .png file but I am not sure what is the exact path I should be giving. I tried different paths that I think would get me to the resource but all return null:

InputStream is = getClass().getClassLoader().getResourceAsStream("playstore.png");
Bitmap bmp = BitmapFactory.decodeStream(is);
mStoreImage.setImageBitmap(bmp);

If I understand correctly I cannot use resources as I am building a .jar. This is my project package structure displayed with Android Studio: enter image description here

Answer

Derek Fung picture Derek Fung · Aug 30, 2015

You should use Resources.openRawResource()

e.g. if your Activity

getResources().openRawResource(R.raw.playstore.png);

remember to put the file under res/raw

Edit:

In addition, android has it own method to get drawable (e.g. a png file) into Bitmap