Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path for getResourceAsStream in Android Project

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

like image 798
2cupsOfTech Avatar asked Aug 30 '15 11:08

2cupsOfTech


2 Answers

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

like image 140
Derek Fung Avatar answered Oct 23 '22 21:10

Derek Fung


Android Studio Gradle projects read files from a separate resources directory in the same way as Maven does it.

Create a "resources" folder in Windows Explorer (Android Studio don't allow you to do that) and then recreate the package path and place files there, e.g.:

A class located at this path:

C:\...\app\src\main\java\com\adscendmedia\videosdk\AdscnedAPI.java

Read resource files from this path:

C:\...\app\src\main\resources\com\adscendmedia\videosdk\playstore.png
like image 1
Stig Helmer Avatar answered Oct 23 '22 21:10

Stig Helmer