processing-android icon indicating copy to clipboard operation
processing-android copied to clipboard

save()

Open SeloSlav opened this issue 9 years ago • 3 comments
trafficstars

What does the save() function do in Android Processing? Can I create a bitmap from my current sketch? How can I then access this bitmap? Otherwise, what is the process for generating a bitmap from my sketch view and storing it for later use in my application?

SeloSlav avatar Sep 13 '16 11:09 SeloSlav

It does save the current content of the sketch view as a file in the internal storage, but thinking about it, should save to the external storage instead so it can be accessed by the user and transferred to the computer. Sounds right?

codeanticode avatar Sep 20 '16 15:09 codeanticode

That would be desirable, yes. As well as being able to share to external applications.

SeloSlav avatar Sep 20 '16 16:09 SeloSlav

From what I read here, it would save to internal storage only if there is no absolute filename specified.

So you should be able to call save() with an absolute path to save to the external storage:

import android.os.Environment;

try {
  if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
    throw new Exception("External storage is not writable.");
  }
 
  File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "your_app_name");
  if (!directory.exists() && !directory.mkdirs()) {
    throw new Exception("Directory could not be created.");
  }
 
  String filename = directory.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg";
  save(filename);
 
  println("Picture has been saved to: " + filename);
} catch (Exception e) {
  println("ERROR: " + e.getMessage());
}

nclavaud avatar Jan 03 '17 21:01 nclavaud