No matter what i do i cannot get access to the data folder in android. Whats wierd is the exact same code works for desktops. I enabled all permissions but it still tells me permission denied. Whats even more wierd is that if I’m not accessing the data folder it works fine. The voided code does not work. Anyway heres my code.
java.io.FileNotFoundException: /data/square.png (Permission denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at processing.core.PApplet.createOutput(PApplet.java:5272)
at processing.core.PImage.save(PImage.java:2842)
at processing.test.save_picture.Save_Picture.setup(Save_Picture.java:29)
at processing.core.PApplet.handleDraw(PApplet.java:1801)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:471)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:503)
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at processing.core.PImage.save(PImage.java:2865)
at processing.test.save_picture.Save_Picture.setup(Save_Picture.java:29)
at processing.core.PApplet.handleDraw(PApplet.java:1801)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:471)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:503)
Hope this helps.
Edit: Of course if you want to save it to your sketch data folder you have to use “/Sketchbook/yourSketch/data” instead of “/imageFolder” If a directory not exist it will be created.
I really appreciate your response. But I’m still getting an error. My sketch name is “Save_Picture” could this problem be my phone isn’t rooted? I’m usually good with desktop processing but when it comes to Android im a total noob.
java.io.FileNotFoundException: /storage/emulated/0/Sketchbook/Save_Picture/data/myImage.png (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at processing.core.PApplet.createOutput(PApplet.java:5272)
at processing.core.PImage.save(PImage.java:2842)
at processing.test.save_picture.Save_Picture.saveImage(Save_Picture.java:48)
at processing.test.save_picture.Save_Picture.setup(Save_Picture.java:33)
at processing.core.PApplet.handleDraw(PApplet.java:1801)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:471)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:503)
Error while saving file: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
Initializing build sequence…
Deleted old build folder
Injected log broadcaster
Detected architecture armeabi-v7a
Packaging resources with AAPT…
Compiling with ECJ…
----------
1. WARNING in /data/user/0/com.calsignlabs.apde/app_build/src/processing/test/save_picture/Save_Picture.java (at line 43)
boolean success = true;
^^^^^^^
The local variable success is never read
----------
1 problem (1 warning)
Dexing with DX Dexer…
Launching sketch preview…
java.io.FileNotFoundException: /storage/emulated/0/imgFolder/myImage.png (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at processing.core.PApplet.createOutput(PApplet.java:5272)
at processing.core.PImage.save(PImage.java:2842)
at processing.test.save_picture.Save_Picture.saveImage(Save_Picture.java:47)
at processing.test.save_picture.Save_Picture.setup(Save_Picture.java:34)
at processing.core.PApplet.handleDraw(PApplet.java:1801)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:471)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:503)
Error while saving file: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
Initializing build sequence…
Deleted old build folder
Injected log broadcaster
Detected architecture armeabi-v7a
Packaging resources with AAPT…
Compiling with ECJ…
----------
1. WARNING in /data/user/0/com.calsignlabs.apde/app_build/src/processing/test/save_picture/Save_Picture.java (at line 43)
boolean success = true;
^^^^^^^
The local variable success is never read
----------
1 problem (1 warning)
Dexing with DX Dexer…
Launching sketch preview…
java.io.FileNotFoundException: /storage/emulated/0/Sketchbook/Save_Picture/data/myImage.png (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at processing.core.PApplet.createOutput(PApplet.java:5272)
at processing.core.PImage.save(PImage.java:2842)
at processing.test.save_picture.Save_Picture.saveImage(Save_Picture.java:47)
at processing.test.save_picture.Save_Picture.setup(Save_Picture.java:34)
at processing.core.PApplet.handleDraw(PApplet.java:1801)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:471)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:503)
Error while saving file: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
Maybe you have to give the sketch the “WRITE_EXTERNAL_STORAGE” permission.
Are you using the APDE app?
There you go to menu/Sketch properties/Sketch Permissions.
On PC I do not remember. I do everything on my tablet.
Please edit your code by choosing the pencil, then select the code and tab the </> Button on the toolbar above your edit box.
I have tried this also on my Lollipop phone now, without failure. But indeed both of them are rooted. I will try tomorrow on an unrooted one.
It’s important for me to know, because a lot of my apps are based on this code.
That’s it. It’s the permission!
I am so used to the preview mode that APDE has ,that I hardly use the app mode. Without permission it really gives these errors.
Why all permissions? I’ve never tried that. Just choose WRITE_EXTERNAL_STORAGE
I’ll try on an unrooted phone.
See below the buttons how they show up on my device.
Just tested it on a not rooted phone Lollipop.
Works perfectly when enabled the write permission. Without this permission it gives exactly the error you posted. Are you using APDE just using this single permission?
Edit: I just realized that you misunderstood when I said “You can edit your code”. I meant the code of your post, not the IDE, because it is somewhat annoying having to scroll so much. The preview mode really has the <> button.
I do clean up old posts regularly – but sometimes this may mean I confuse new forum users into thinking that forum formatting happens automatically. :shrug: not anyone’s fault, but it is good to politely remind everyone to format their code; thanks for doing that.
@PhoenixStormJr
I think I know where things went wrong. I’ll try to make it as simple as possible.
In the code below I will only use two lines of code to save the image. The first line uses an android function to get the absolute path. When you open the Android File Explorer you will see folders above the absolute path, like DCMI or Sketchbook. But there are underlying folders, and that is why you need to call the absolute folder path. Then you add the resting folders above that level. That is what the first line does. Note that the folders are between backslashes.
You have to verify that the folders really exist, otherwise you have to create them first. Just change the folder name of your sketch and the image.
You can save the file as PImage or PGraphics. The first I only used to show the image as soon as created.
Now I think, things are good to go.
You can download the code here
I reinstalled processing and used your code but i am still getting errors. I also reapplied my permissions. It doesn’t seem like I’m getting anywhere and I’m beggining to think it’s impossible for Samsung galaxy phones especially since I dont even have access to the processing folder for stupid non root.
Unrelated: (And for the </> button when I copy code it copies in a straight line and i have to press enter 100 times to get any of it to work so sorry if i made anyone angry)