text saver. Save text to any location in android. Please set the manifest permissions by adding the following file to your sketch folder.
AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="">
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="28"/>
<application android:icon="@mipmap/ic_launcher" android:label="">
<activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import android.app.Activity;
import android.content.Context;
import android.os.Environment;
Activity activity;
Context context;
Permission p;
String s1 ,s2 = "iuiugifuy";
String absolutePath;
public boolean overWrite = true;
public void setup(){
size(600,600);
p = new Permission(this,"WRITE_EXTERNAL_STORAGE");
activity = this.getActivity();
context = activity.getApplicationContext();
absolutePath = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
};
public void draw(){
background(255);
if(s1!=null){
fill(0);
text(s1,20,20);
}else{
fill(0);
text("no file",20,20);
}
};
void mousePressed(){
generateNoteOnSD(context,"test.txt",s2+hour()+":"+minute()+":"+second());
readFromFile(context);
};
public void generateNoteOnSD(Context context, String FileName, String text) {
try {
//
File root = new File(absolutePath, "Notes");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, FileName);
//set to false will overwrite the file, set to true to append
FileWriter writer = new FileWriter(gpxfile,false);
//use write or append
writer.append(text);
// use \n to add new line
writer.append("\n");
writer.flush();
writer.close();
println("write file");
} catch (IOException e) {
e.printStackTrace();
}
};
private String readFromFile(Context context) {
FileInputStream fis = null;
try {
fis = new FileInputStream (new File(absolutePath+"/notes/"+"test.txt"));
InputStreamReader isr = new InputStreamReader(fis);
// READ STRING OF UNKNOWN LENGTH
StringBuilder sb = new StringBuilder();
char[] inputBuffer = new char[2048];
int l;
// FILL BUFFER WITH DATA
while ((l = isr.read(inputBuffer)) != -1) {
sb.append(inputBuffer, 0, l);
println("write data",inputBuffer, 0, l);
}
// CONVERT BYTES TO STRING
s1 = sb.toString();
fis.close();
}catch (Exception e) {
println("cannot fetch file",e);
} finally {
if (fis != null) {
fis = null;
}
}
return s1;
};
image saver. Save image to any location in android.
import android.os.Environment;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import processing.core.PConstants;
import android.app.Activity;
PImage img;
Activity activity;
boolean image_saved;
boolean permissions_granted;
// Folowing file name is the folder you save your image file in.
// If it does not exist it will be created automatically
String folder_name = "MyImageFolder";
// Folowing string is the name of your image file
// Be sure to include extension.
String image_file = "MyImage.jpg";
Permission wstorage,rstorage;
void setup() {
wstorage = new Permission(this,"WRITE_EXTERNAL_STORAGE");
rstorage = new Permission(this,"READ_EXTERNAL_STORAGE");
size(displayWidth, displayHeight);
background(0, 0, 200);
rectMode(CENTER);
rect(width/2, height/2, width, height/2);
textAlign(CENTER);
String str = "Click to save an image.";
textSize(12);
float twf = width/textWidth(str);
textSize(8*twf);
fill(0);
text(str, width/2, height/2);
img = get();
//img.save(
};
void draw() {
if (image_saved) {
background(0, 0, 200);
fill(255);
text("Image saved.", width/2, height/2);
}
};
void mousePressed() {
saveMyImage(folder_name, image_file);
};
void saveMyImage(String sf, String tf) {
try {
String absolute_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
File file = new File(absolute_path+"/"+sf);
//println(file);
if (!file.exists()) {
boolean success = true;
success = file.mkdirs();
if(success){
save(file+"/"+tf);
image_saved = true;
println("File saved successfully.");
println("Path",absolute_path,"File",file);
}
else {
println("no File");
println("Path",absolute_path,"File",file);
}
}
}
catch (Exception e) {
//println("Error while saving file: " + e);
}
};
image saver as a class if image with a matching filename is found the file is then numbered before saving;
import android.os.Environment;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
imageSaver img;
void setup(){
size(displayWidth, displayHeight);
background(0, 0, 200);
rectMode(CENTER);
rect(width/2, height/2, width, height/2);
textAlign(CENTER);
String str = "Click to save an image.";
textSize(12);
float twf = width/textWidth(str);
textSize(8*twf);
fill(0);
text(str, width/2, height/2);
img = new imageSaver(this);
};
void draw(){
img.logic();
};
class imageSaver{
Permission wstorage,rstorage;
Activity act;
PApplet p;
int counter = -1,maxImageCount;
boolean imageSaved,mdown;
String folderName = "MyImageFolder",fileName,ext,absolutePath;
String imageFile = "MyImage.jpg";
PImage img;
File file,file2;
imageSaver(PApplet applet){
p = applet;
wstorage = new Permission(p,"WRITE_EXTERNAL_STORAGE");
act = p.getActivity();
getExt(imageFile);
};
imageSaver(PApplet applet,String s1, String s2){
folderName = s1;
imageFile = s2;
p = applet;
wstorage = new Permission(p,"WRITE_EXTERNAL_STORAGE");
act = p.getActivity();
getExt(imageFile);
};
public void logic() {
if(mousePressed&&!mdown) {
img = p.get();
checkLocation(folderName, imageFile);
mdown = true;
counter++;
}
if(!mousePressed)mdown = false;
};
public void logic(PGraphics canvas) {
if(true) {
img = canvas.get();
checkLocation(folderName, imageFile);
counter++;
}
};
public void checkLocation(String sf, String tf) {
boolean k = false;
try {
absolutePath = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
file = new File(absolutePath+"/"+sf);
//PApplet.println("checking file1",file,counter);
if (!file.exists()&&counter==-1) {
boolean success = true;
success = file.mkdirs();
img.save(file+"/"+fileName+"."+ext);
} else k = true;
if(counter>-1)k=true;
boolean k1 = false;
while(k&&counter<100&&!k1) {
try {
file2 = new File(absolutePath+"/"+folderName+"/"+fileName+counter+"."+ext);
PApplet.println("checking file2",file2);
if (file2.exists()) {
counter++;
} else {
k = false;
k1 = true;
break;
}
}
catch (Exception e) {
PApplet.println("Error while saving file: " + e);
}
}
if(k1){
//PApplet.println("file",file);
//PApplet.println("Fname",folderName);
//PApplet.println("fileName",fileName);
//PApplet.println("counter",counter);
//PApplet.println("ext",ext);
if(counter>-1)img.save(file+"/"+fileName+counter+"."+ext);
imageSaved = true;
//PApplet.println("File saved successfully.");
}
}
catch (Exception e) {
PApplet.println("Error while saving file: " + e);
}
};
void getExt(String location){
int count = 0;
fileName = location.substring(0,location.indexOf("."));
ext = location.replace(fileName,"");
ext = ext.replace(".","");
ext = ext.replace(fileName,"");
};
public File[] listFiles(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
File[] files = file.listFiles();
return files;
} else {
// If it's not a directory
return null;
}
};
};
public class Permission{
String permissionName;
PApplet parent;
public Permission(PApplet pParent,String permissionName) {
parent = pParent;
this.permissionName = permissionName;
parent.requestPermission("android.permission."+permissionName, "onPermissionResult", this);
};
public void onPermissionResult(boolean granted) {
if (!granted) {
PApplet.println("User did not grant camera permission. "+ permissionName +" is disabled.");
}
};
};