Accessing `res/drawable' from loadImage()


Sorry if this is a stupid question. I’ve tried searching for an answer but have come up empty handed.

I haven’t done any Android dev in maybe 3 years, and I generally just use the Processing IDE for any Processing stuff I make. Currently I’m working in Android Studio as I need a bit more functionality than what the PDE offers.

In my I need to load some images, which I have stashed in res/drawable (the default location) and am wondering how I access those from loadImage().

PImage img;
img = loadImage(R.drawable.IMAGE); 
// img = loadImage("R.drawable.IMAGE")

Neither of those work, and was just wondering if someone knows how to access that directory. Or if there’s a better solution.



@derekkinsman ===

your way is the standard Android way (based on xml layout) but it does not work with P5; with P5 you have to put the images in the data folder then acceed them with AssetManager== look to this forum or the previous one, i have already put code snippet for that.


Is there a reason why loadImage() works from the PDE on an Android, but doesn’t work when I use it within Android Studio?


@derekkinsman === in order to answer i have to guess what you are doing (or what you have done)

  • i suppose that you created an app with P5 && android mode; this app uses an image which is in the data folder. But when compiling your image is put inside your apk and it cannot be seen (when putting the file in thestandard location)

  • then i suppose that you export it “for android”

  • then i suppose that you import it as a projetct into AS; till now tell me wether i am right?

  • supposing that i am right (!) go to your drawable folder: you can see that there is not any image here (nor in the drawable res from android); of course you can add it before importing to AS, in this case you can find your image in “drawable” AS : Problem is that you cannot use it with standard P5 code (loadImage)

  • you can also try to find the image from the folder created by P5 while compiling; this path is easy to find with getFilesDir().getPath(). Yet you cannot use it because of the fileSeparator error that a filename cannot include and P5 loadImage uses fileInput() which does not allow fileSeparators…

  • there are solutions (a lot of) but as for me the most simple (using AS) is to create an assets folder and put your image there; then call loadImage(“yourimage.jpg”) and see what happens = it works!


@akenaton oh brilliant. Yes, create assets directory, then loadImage(), that’s exactly what I wanted. Thanks a lot!

I was trying to struggle with the assetmanager and all that.

I didn’t need anything to stay in the “data” directory of a processing sketch. I just didn’t understand the different approaches between the PDE and AS.

Thanks again mate!


@derekkinsman ====
happy to help
(AssetManager is very useful in many cases, dont let it back!)…