Android Cannot get access to data folder

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.

PImage save;
PGraphics square;


void setup(){
square = createGraphics(100,100);
square.beginDraw();
rect(0,0,100,100);
square.endDraw();
save = square.get();
//save.save("/data/square.png");
save.save("square.png");
}

void draw(){
image(save,0,0);
}

Keeps giving me this error message

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)
1 Like

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.

import android.os.Environment;

PImage save;
PGraphics square;

String s = "myImage.png";

void setup() {
  size(200, 200);
  square = createGraphics(100, 100);
  square.beginDraw();
  square.fill(0, 0, 150);
  square.rect(0, 0, 100, 100);
  square.endDraw();
  image(square, 0, 0);
  save = get(0, 0, 100, 100);
  saveImage(s);
}

void draw() {
}
 

  void saveImage(String s) {
   try
      {
     String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/imgFolder");
     File imgFolder = new File(directory);
      if (!imgFolder.exists()) {
      boolean success = true;
       success = imgFolder.mkdirs();
    }
      save.save(directory + "/" + s);
          println("File saved successfully.");
  } 
     catch (Exception e) 
        {
       println("Error while saving file: " + e);
  }
}
1 Like

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

What did the try catch code print?
Could you post your whole code?

//Sorry it took so long i have college
//I used your code

import android.os.Environment;
PImage save; 
PGraphics square; 
String s = "myImage.png"; 
void setup() { 
size(200, 200); 
square = createGraphics(100, 100); 
square.beginDraw(); 
square.fill(0, 0, 150); 
square.rect(0, 0, 100, 100); 
square.endDraw(); 
image(square, 0, 0); 
save = get(0, 0, 100, 100); 
saveImage(s); 
} 
void draw() { } 
void saveImage(String s) { 
try {
String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/imgFolder"); 
File imgFolder = new File(directory); 

if (!imgFolder.exists()) { 
boolean success = true; 
success = imgFolder.mkdirs(); 
} 

save.save(directory + "/" + s); 
println("File saved successfully."); 
}

catch (Exception e) { 
println("Error while saving file: " + e); 
} } 

//And got this error

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

//I Also used this

import android.os.Environment;
PImage save;
PGraphics square; 
String s = "myImage.png"; 
void setup() { 
size(200, 200); 
square = createGraphics(100, 100); 
square.beginDraw(); 
square.fill(0, 0, 150); 
square.rect(0, 0, 100, 100); 
square.endDraw(); 
image(square, 0, 0); 
save = get(0, 0, 100, 100); 
saveImage(s); 
} 
void draw() { } 
void saveImage(String s) { 
try {
String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Sketchbook/Save_Picture/data"); 
File imgFolder = new File(directory); 

if (!imgFolder.exists()) { 
boolean success = true; 
success = imgFolder.mkdirs(); 
} 

save.save(directory + "/" + s); 
println("File saved successfully."); 
}

catch (Exception e) { 
println("Error while saving file: " + e); 
} } 

//And the same error

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.

I have all permissions enabled. I also do not see the </> button.

Is the button <> because I do not see </>?

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.:blush:

2 Likes

I tried that one permission… guess its just my phone sucks. Thank you for trying and i see the </> button.

good, can you PLEASE use it to repair all your above posts? about CODE or ERROR

and i check older posts like
Multiple Sine Waves ,
and WOW good formatting,

but then i see moderator @jeremydouglass did it for you!
and you not learned anything!

1 Like

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.

1 Like

@PhoenixStormJr Try reinstalling APDE or Android mode on PC.

1 Like

@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

.

import android.os.Environment;

PImage myImage;
PGraphics img;

void setup() {
  size(200, 200);
  img = createGraphics(200, 200);
  img.beginDraw();
  img.fill(0, 0, 150);
  img.rect(0, 0, 200, 200);
  img.textSize(30);
  img.textAlign(CENTER,CENTER);
  img.fill(255);
  img.text("My Image",img.width/2, img.height/2);
  img.endDraw();
  image(img, 0, 0);
  myImage = get(0, 0, 200, 200);
  image(myImage, 0, 0);
  
  String directory_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/sketchBook/mySketchFolder/data/");
  img.save(directory_path + "myImageName.png");
}
1 Like

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)

I am also on android so when I tried to download it I got “error no app capable of opening file…” or something.