Android Cannot get access to data folder

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