I am using an external .txt file to save the incrementing name index for whenever someone "takes a picture" in my app ( i.e. image_1.jpg, image_2.jpg etc...). I am trying to save the data externally so that a user does no overwrite their pictures each time they run the program. However, because of the way that Processing packages its contents for export I cannot both read and write to the same file. It reads the appropriate file located in the apps package contents, however, when It tries to write to it it creates a new folder in the same directory as the app itself and writes a new file with the duplicate name to the one it is trying to read/write to.
Essentially, it reads the proper file but refuses to write to it, instead making a copy and writing to that one. The app runs fine but every time you open it and take pictures you overwrite the images you already had. I have tried naming the "write to" location the explicitly same link as where the exported app stores the data folder inside the package contents (Contents/Resources/Java/data/assets) but this creates a copy of this directory in the same file as the app. I have also tried excluding the file I am trying to read/write from my data folder when I export the app by changing the read code to "../storage/pictureNumber.txt" and then putting this file next to app itself. When I do this the app doesn't launch at all because it is looking in its own data folder for storage and refuses to go outside of itself with ../ . Has anyone had luck both reading from and writing to the same file in an exported processing .app?
Here is the code for the class that is handling the loading and saving of the file:
- class Camera {
- PImage cameraImage;
- int cameraPadding = 10;
- int cameraWidth = 60;
- int opacity = 0;
- int flashDecrementer = 50; //higher number means quicker flash
- int pictureName;
- Camera() {
- try {
- String[] pictureIndex = loadStrings("/storage/pictureNumber.txt");
- int pictureName = int(pictureIndex[0]);
- cameraImage = loadImage("assets/camera.jpg");
- String _pictureName = ""+char(pictureName);
- println(pictureName);
- }
- catch(NullPointerException e) {
- String[] pictureIndex = {
- "1"
- };
- saveStrings("storage/pictureNumber.txt", pictureIndex);
- int pictureName = int(pictureIndex[0]);
- cameraImage = loadImage("assets/camera.jpg");
- String _pictureName = ""+char(pictureName);
- println(pictureName);
- }
- }
- void display(float mx, float my) {
- image(cameraImage, cameraPadding, cameraPadding, cameraWidth, cameraWidth-cameraWidth/5);
- }
- boolean isOver(float mx, float my) {
- if (mx >= cameraPadding &&
- mx <= cameraPadding+cameraWidth &&
- my >= cameraPadding &&
- my <= cameraPadding+cameraWidth-cameraWidth/5) {
- return true;
- }
- else {
- return false;
- }
- }
- void captureImage() {
- save("pictures/"+lines.picturePrefix+"_"+pictureName+".jpg");
- pictureName++;
- String _null = "";
- // String _tempPictureName = _null.valueOf(pictureName);
- String[] _pictureName = {
- _null.valueOf(pictureName)
- };
- saveStrings("storage/pictureNumber.txt", _pictureName);
- println(_pictureName);
- }
- void flash() {
- fill(255, opacity);
- rect(0, 0, width, height);
- opacity -= flashDecrementer;
- if (opacity <= 0) opacity = 0;
- }
- }