saveFrame in Android (code share)

The saveFrame function does not work on Android.
Here is a workaround.

import android.os.Environment;

int tel = 0, x; 
boolean no_loop = true;
String my_folder = "my_frames"; // Must be an existing folder one level above absolute path
String data_folder_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+my_folder+"/");
PImage pi;

void setup() {
  size(100, 100);
}

void draw() { 
  if (no_loop) {
    background(204); 
    if (x < 100) { 
      line(x, 0, x, 100); 
      x = x + 1;
    } else { 
      no_loop = false;
    } // Saves each frame as line-000001.png, line-000002.png, etc. 
    saveFrame("line-"); // numbers(hashtags) will be included in saveFrame()
  }
}

void saveFrame(String s) {
  int maxHashes=6; 
  tel++;
  String fs = String.format("%1$" + maxHashes + "s", tel).replace(' ', '0');
  pi = get(0, 0, width, height);
  pi.save(data_folder_path+s+fs+".png");
}  
2 Likes