Where to store images for loadImage("image.png")?

edited December 2017 in Android Mode

I'm having trouble loading my images when working in Android Studio. I had the same problem when I was working with plain Java, so I know it just comes down to where I store the files. Based on (possibly outdated) answers to basically the same question, I've tried creating folders named 'data,' 'assets,' and 'files' in various locations in my project, as well as the built-in folder 'build/generated/assets.' Seems like maybe there's a new answer to this?

Here is what appears to be the relevant line from the stacktrace:

W/System.err: java.io.FileNotFoundException: /data/data/com.wordpress.deivescollins.blackcauldron/files/cauldron.png: open failed: ENOENT (No such file or directory)

TL;DR Where do my images go when I'm working with Android Studio?

Answers

  • edited December 2017

    @Rammschnev===

    • using processing you put your image in the data folder, as usually, then use AssetManager to get the path to your image, then load it from this path (you can also create a subfolder and get the path to its contents with the same technique)

    • using AS or Eclipse you have more choice:

    a) you can put your images in res (ldpi,mdpi,highdpi.....) according to your needs and images b) you can put them in the assets folder c) you can put them on the sdcard; d) you can also create (if it does not exist) a folder called raw, but normally this is for text or sound or videos files);

    in each of this case in order to get your files you have to use different ways (getAssets....fileInputStream....);

  • edited December 2017

    @akenaton

    Thanks so much for the input. I only really have one image, and I do my scaling for different screen sizes within my code, so I'm interested in the most straight-forward approach possible. Two questions:

    • Which folder does loadImage() look in?
    • I keep seeing people talking about the assets folder and not saying any more than that. Is that the one I had referred to, build/generated/assets?
  • So that is somewhat of a lead. I've figured out what's meant by the assets folder, and I've dug inside of the core Processing library to find the folder where loadImage() is looking, which I expected to be somewhere in my source but is actually on the target device, under /data/data/com.mypackage.etc/files/. This is the path that was returned in the original stacktrace, but I mistook it for a relative path because it didn't begin with C:\, when in fact it is the sketchPath variable in my PApplet instance.

    So, is there a straightforward way to get my file there? It would be cleanest if there were a way to have the APK place it there during installation, but I could also get away with getting it there during setup(). I'm going to be tinkering with this myself, so I'll come back and report if I'm successful. You'd think this would be a fairly simple operation, wouldn't you?

  • edited December 2017 Answer ✓

    This did the trick:

    public void setup() { // Copy images from assets to sketchPath File resource = new File(sketchPath + "/" + FFTCauldron.FILE_NAME); OutputStream out = null; InputStream source = null; if (!resource.exists()) { try { source = getActivity().getAssets().open(FFTCauldron.FILE_NAME); out = new FileOutputStream(resource); byte[] buffer = new byte[1024]; int length; while ((length = source.read(buffer)) > 0) out.write(buffer, 0, length); } catch (Exception e) { Log.e("IO", "" + e); } finally { try { source.close(); out.close(); } catch (Exception e) { Log.e("IO", "" + e); } } } }

    And for anybody else who doesn't know what the assets folder is: https://stackoverflow.com/questions/26706843/adding-an-assets-folder-in-android-studio#34315335

  • because it didn't begin with C:\,

    This is only valid when running on a Windows OS. However, Android is a linux variant. All folders are expressed based on the root path designated as "/" (In Windows, the equivalent would be C: as you have stated)

    Now, on Android you are not allowed to access any path. For instance, you can write to the SD card only if you have that permission enabled. However, all apps can only write to their designated location which is defined when the application is exported.

    I believe if you use dataPath("image_file.jpg"), that function should get you access to your "image_file.jpg". That function retrieves the path of this file in your phone. You don't need to know where that file is located in your phone. If you call that function, you will get it. When you run the application in your device from the Processing IDE, the only thing you need to make sure is to place this file in the data folder. When you run and build your application, Processing will export your recently generated apk file (and its resources) to a specific location in your device. No matter where it is place, your program will (should) be able to access this and any other resource file(movies, sound, data, etc) using dataPath().

    Now, I just misread your question. You are interested in AS. I have to check this, by if you get a reference to your current context, then you can do something like cont.getAssets("file_to_retrieve");. I think that should work.

    Kf

  • edited December 2017

    @Rammschnev===

    yes: getAssets() + javaIO Stream...

Sign In or Register to comment.