# Android Cannot get access to data folder

**URL:** <https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636>\
**Category:** Processing for Android\
**Created:** [October 12, 2019, 5:46pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636 "2019-10-12T17:46:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 12, 2019, 5:46pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/1 "2019-10-12T17:46:19Z")

</div>

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.

```auto
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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 12, 2019, 8:21pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/2 "2019-10-12T20:21:43Z")

</div>

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.

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 13, 2019, 12:10am UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/3 "2019-10-13T00:10:18Z")

</div>

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 13, 2019, 2:55am UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/4 "2019-10-13T02:55:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 16, 2019, 9:34pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/5 "2019-10-16T21:34:59Z")

</div>

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

```auto
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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 16, 2019, 9:39pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/6 "2019-10-16T21:39:10Z")

</div>

//I Also used this

```auto
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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 16, 2019, 10:56pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/7 "2019-10-16T22:56:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 16, 2019, 11:26pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/8 "2019-10-16T23:26:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 16, 2019, 11:49pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/9 "2019-10-16T23:49:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 17, 2019, 6:07pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/10 "2019-10-17T18:07:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 17, 2019, 6:08pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/11 "2019-10-17T18:08:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 17, 2019, 8:03pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/12 "2019-10-17T20:03:34Z")

</div>

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.

 ![Screenshot_2019-10-17-16-56-54](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5c42cbb16f08a3cc935316aa2f7b4be29124d813.png)

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 18, 2019, 2:55pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/13 "2019-10-18T14:55:39Z")

</div>

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.😊

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 19, 2019, 3:01am UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/14 "2019-10-19T03:01:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 19, 2019, 3:14am UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/15 "2019-10-19T03:14:23Z")

</div>

> [@PhoenixStormJr](#):
>
> 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 - #2 by PhoenixStormJr](https://discourse.processing.org/t/multiple-sine-waves/14332/2) ,  
and WOW good formatting,

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

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [October 19, 2019, 3:40am UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/16 "2019-10-19T03:40:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 19, 2019, 7:51am UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/17 "2019-10-19T07:51:27Z")

</div>

@PhoenixStormJr Try reinstalling APDE or Android mode on PC.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 19, 2019, 11:34pm UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/18 "2019-10-19T23:34:09Z")

</div>

@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](https://drive.google.com/file/d/1K0fvHMi7gWYeCHZ-kQ-yNXxtO5S1AqMZ/view?usp=drivesdk)

.

```auto
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");
}

```

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 20, 2019, 3:45am UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/19 "2019-10-20T03:45:37Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 20, 2019, 3:53am UTC](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636/20 "2019-10-20T03:53:25Z")

</div>

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

[Next page](https://discourse.processing.org/t/android-cannot-get-access-to-data-folder/14636.md?page=2)
