# No serial library Processing for smartphone. Help

**URL:** https://discourse.processing.org/t/no-serial-library-processing-for-smartphone-help/29870
**Category:** Processing for Android
**Created:** [May 5, 2021, 9:34am UTC](https://discourse.processing.org/t/no-serial-library-processing-for-smartphone-help/29870 "2021-05-05T09:34:04Z")
**Posts on this page:** 1
**Showing post:** 162

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [May 13, 2021, 12:57am UTC](https://discourse.processing.org/t/no-serial-library-processing-for-smartphone-help/29870/162 "2021-05-13T00:57:46Z")

</div>

furthermore here is a sketch I posted a few days back which allows you to do exactly this.

you should be able to use the first sketch posted.

> [@Read and write images and text files Android](https://discourse.processing.org/t/read-and-write-images-and-text-files-android/29947):
>
> 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:n…

just set the following line in setup

my full updated code in case that last link is problematic.

> **Summary**
>
> ```auto
> 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;
> import android.content.DialogInterface;
> import android.graphics.Color;
> import android.widget.TextView;
> import android.view.Gravity;
> import processing.core.PApplet;
> import android.app.Activity;
> import android.app.AlertDialog;
> 
> fileOutput f;
> 
> String s2 = "oifoifnowibf";
> void settings() {
> size(600, 600, P2D);
> };
> 
> public void setup() {
> 
> f = new fileOutput(this);
> f.setLocation("notes/test.txt");
> f.setAndroidDialogue("No","Yessss");
> };
> 
> public void draw() {
> background(255);
> if (f.fileContent!=null) {
> fill(0);
> text(f.counter, 20, 10);
> text(f.fileContent, 20, 20);
> } else {
> fill(0);
> text("no file", 20, 20);
> }
> };
> 
> void mousePressed() {
> if (f.writeFile) {
> f.writeLn(s2+hour()+":"+minute()+":"+second());
> f.close();
> f.loadStrings();
> }
> if(!f.writeFile)f.dialogBox();
> };
> 
> ```

> **Summary**
>
> class fileOutput {  
> public PApplet applet;  
> public FileWriter output;  
> float x, y, w, h;  
> public boolean save, onMouseUp, mdown, debug, append = true, appendFile, match,  
> append2, overWrite = true, overWriteOnce = true, writeOnce, writeFile, click,  
> getPermission = true, fileExists, reWrite;  
> public int counter=-1, counter2;  
> public File file, file2, file3;  
> public File SDcards ;  
> public String location, filePath, folderPath = “”;  
> String text = “oioijsofoivnsoindv”, absolutePath, ext, fileName, fileContent = “”;  
> String androidDialogueTitle = “Would you like to overWrite”,  
> dialogueB1Title = “”,dialogueB2Title = “”,dialogueBody;  
> Permission p;  
> Activity activity;  
> Context context;  
> TextView msg;  
> int msgId;
> 
> fileOutput() {  
> };
> 
> fileOutput(boolean a, PApplet applet) {
> 
> ```
> this.applet = applet;
> p = new Permission(applet, "WRITE_EXTERNAL_STORAGE");
> overWrite = true;
> appendFile = true;
> 
> ```
> 
> };
> 
> fileOutput(PApplet app) {  
> applet = app;  
> p = new Permission(applet, “WRITE\_EXTERNAL\_STORAGE”);  
> //init();  
> };  
> //currently unused  
> fileOutput(String location, PApplet applet) {
> 
> ```
> this.applet = applet;
> p = new Permission(applet, "WRITE_EXTERNAL_STORAGE");
> setLocation(location);
> init();
> 
> ```
> 
> };
> 
> fileOutput(PApplet applet, String location) {
> 
> ```
> this.applet = applet;
> p = new Permission(applet, "WRITE_EXTERNAL_STORAGE");
> setLocation(location);
> init();
> 
> ```
> 
> };
> 
> void getAndroidInfo() {  
> activity = applet.getActivity();  
> context = activity.getApplicationContext();  
> absolutePath = new String(Environment.getExternalStorageDirectory().getAbsolutePath());  
> };
> 
> void init() {  
> x = 0;  
> y = 0;  
> w = applet.width;  
> h = applet.height;
> 
> ```
> String s1 = "Would you like to overWrite "+fileName+"."+ext+"?";
> float dboxWidth = 120;
> float tSize = 20;
> applet.textSize(tSize);
> float dw = applet.textWidth(s1)+100;
> float dboxHeight = 150;
> float dx = applet.width/2-dw/2;
> float dy = applet.height/2-50/2;
> 
> ```
> 
> };
> 
> void loadStrings() {  
> loadFile(context);  
> };
> 
> String loadFile(Context context) {
> 
> ```
> FileInputStream fis = null;
> if (writeFile) {
> try {
> if (!reWrite)fis = new FileInputStream (new File(file2.getAbsolutePath()));
> else fis = new FileInputStream (new File(file3.getAbsolutePath()));
>     
> 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
> fileContent = sb.toString();
> 
> fis.close();
> }
> catch (Exception e) {
> println("cannot fetch file", e);
> } 
> finally {
> if (fis != null) {
> 
> fis = null;
> }
> }
> }
> return fileContent;
> 
> ```
> 
> };
> 
> void open() {  
> };
> 
> void close() {  
> try {  
> output.flush();  
> output.close();  
> }  
> catch(IOException e) {  
> }  
> };
> 
> void write(String s) {
> 
> ```
> if (!overWrite)checkLocation();
> try {
> 
> output = new FileWriter(file2, append);
> }
> catch(IOException e) {
> }
> printWrite(s);
> 
> ```
> 
> };
> 
> void writeLn(String s) {  
> if (!overWrite)checkLocation();  
> try {  
> output = new FileWriter(file2, append);  
> }  
> catch(IOException e) {  
> }  
> printWriteLn(s);  
> };
> 
> void write(String s) {  
> if (!overWrite)checkLocation();  
> try {  
> output = new FileWriter(file2, append);  
> }  
> catch(IOException e) {  
> }  
> printWrite(s);  
> };
> 
> void writeLn(String s) {  
> if (!overWrite)checkLocation();  
> try {  
> output = new FileWriter(file2, append);  
> }  
> catch(IOException e) {  
> }  
> printWriteLn(s);  
> };
> 
> void printWrite(String s) {  
> if (!overWrite)checkLocation();  
> try {  
> output.append(s);  
> }  
> catch(IOException e) {  
> }  
> };
> 
> void printWriteLn(String s) {  
> if (!overWrite)checkLocation();  
> try {  
> output.append(s);  
> output.append(“\n”);  
> }  
> catch(IOException e) {  
> }  
> };
> 
> void printWrite(String s) {  
> if (!overWrite)checkLocation();  
> try {  
> for (int i=0; i\<s.length; i++) {  
> output.append(s[i]);  
> }  
> }  
> catch(IOException e) {  
> }  
> };
> 
> void printWriteLn(String s) {  
> if (!overWrite)checkLocation();  
> try {  
> for (int i=0; i\<s.length; i++) {  
> output.append(s[i]);  
> output.append(“\n”);  
> }  
> }  
> catch(IOException e) {  
> }  
> };
> 
> void checkLocation() {  
> boolean k = false;  
> String s1 = “Would you like to overWrite “+fileName+counter+”.”+ext+“?”;  
> try {  
> //  
> file = new File(absolutePath, folderPath);  
> if (!file.exists()&&counter==-1) {  
> file.mkdirs();  
> file2 = new File(file, “/”+fileName+“.”+ext);  
> androidDialogueTitle = file.getAbsolutePath();  
> PApplet.println(“checking file1”, file2);  
> if (file2.exists()) {  
> counter ++;
> 
> ```
> PApplet.println("File Exists");
> //dbox.main.title.label = s1;
> }
> } else if (counter==-1) {
> fileExists = true;
> counter = 0;
> k = true;
> } else k = true;
> if (overWrite&&writeOnce) {
> k=false;
> } else if (overWrite&&!writeOnce&&writeFile) {
> writeOnce = true;
> k = true;
> }
> boolean k1 = false;
> if (writeFile)
> while (k&&counter<100&&!k1) {
> s1 = fileName+counter+"."+ext+"?";
> try { 
> 
> file2 = new File(absolutePath+"/"+folderPath+"/"+fileName+counter+"."+ext);
> file3 = new File(absolutePath+"/"+folderPath+"/"+fileName+(counter)+"."+ext);
> androidDialogueTitle = file3.getAbsolutePath();
> if (file2.exists()) { 
> counter++;
> } else {
> PApplet.println("checking file2", file2);
> //output = new FileWriter(file2, append);
> k = false;
> k1 = true;
> break;
> }
> } 
> catch (Exception e) { 
> PApplet.println("Error while saving file: " + e);
> }
> }
> if (k1||!writeFile) {
> //PApplet.println("file",file);
> //PApplet.println("Fname",folderName);
> //PApplet.println("fileName",fileName);
> //PApplet.println("counter",counter);
> //PApplet.println("ext",ext);
> if (file2==null&&counter<0)file2 = new File(file+"/"+fileName+"."+ext); 
> else if (file2==null)file2 = new File(file+"/"+fileName+"."+ext); 
> if (output==null) {
> //img.save(file+"/"+fileName+counter+"."+ext);
> 
> if (writeFile)output = new FileWriter(file2, append);
> PApplet.println("File saved successfully.");
> //use write or append
> }
> }
> if (reWrite) {
> file2 = new File(file+"/"+fileName+counter+"."+ext); 
> output = new FileWriter(file2, append);
> }
> println("write file", folderPath, fileName, counter, ext);
> if (reWrite)overWrite = true;
> } 
> catch (IOException e) {
> e.printStackTrace();
> }
> 
> ```
> 
> };
> 
> void listen() {  
> //if(writeFile&&dbox.main.getButton(0).click)checkLocation();  
> };
> 
> void setLocation(String s) {  
> if (getPermission)getAndroidInfo();  
> location = s;  
> folderPath = s.substring(0, s.lastIndexOf(“/”));  
> fileName = s.replace(folderPath+“/”, “”);  
> getExt(fileName);  
> PApplet.println(“Fname”, folderPath);  
> PApplet.println(“fileName”, fileName);  
> PApplet.println(“counter”, counter);  
> PApplet.println(“ext”, ext);  
> checkLocation();  
> init();  
> }
> 
> void getExt(String location) {
> 
> ```
> int count = 0;
> fileName = location.substring(0, location.indexOf("."));
> ext = location.replace(fileName, "");
> ext = ext.replace(".", "");
> ext = ext.replace(fileName, "");
> 
> ```
> 
> };
> 
> void dialogBox() {
> 
> ```
> msg = new TextView(activity); 
> msg.setBackgroundColor(Color.BLUE);
> msg.setTextSize(32);
> msg.setText(dialogueBody); 
> msg.setGravity(Gravity.CENTER_HORIZONTAL); 
> msg.setTextColor(Color.WHITE); 
> 
> activity.runOnUiThread(new Runnable() {
> public void run() {
> AlertDialog.Builder builder = new AlertDialog.Builder(activity);
> builder.setView(msg);
>     
> androidDialogueTitle += " "+fileName+"."+ext+"?";
> builder.setTitle(androidDialogueTitle);
> builder.setPositiveButton(dialogueB1Title, 
> new DialogInterface.OnClickListener() {
> public void onClick(DialogInterface dialog, 
> int which) {
> writeFile = true;
> overWrite = false;
> reWrite = true;
>           
> }
> }
> );
> builder.setNegativeButton(dialogueB2Title, 
> new DialogInterface.OnClickListener() {
> public void onClick(DialogInterface dialog, 
> int which) {
> writeFile = true;
> overWrite = true;
> //act.finish();
> }
> }
> )
> .show();
> }
> }
> );
> 
> ```
> 
> };
> 
> void setAndroidDialogue(String s1,String s2){  
> dialogueB1Title = s1;  
> dialogueB2Title = s2;
> 
> };
> 
> void setAndroidDialogue(String s1,String s2,String s3){  
> androidDialogueTitle = s1;  
> dialogueB1Title = s2;  
> dialogueB2Title = s3;
> 
> };
> 
> void setAndroidDialogueTitle(String s1){  
> androidDialogueTitle = s1;  
> };
> 
> void setDialogueColor(){
> 
> };
> 
> public boolean click() {  
> boolean k = false;  
> if (pos()&&applet.mousePressed&&!click) {  
> click = true;  
> k = false;  
> } else if (click&&!applet.mousePressed) {  
> k = true;  
> click = false;  
> }  
> return k;  
> };
> 
> public boolean pos(PVector mouse) {  
> return mouse.x\>x&&mouse.x\<x+w&&mouse.y\>y&&mouse.y\<y+h;  
> };
> 
> public boolean pos() {  
> return applet.mouseX\>x&&applet.mouseX\<x+w&&applet.mouseY\>y&&applet.mouseY\<y+h;  
> };  
> };

Permissions

> **Summary**
>
> ```auto
> public class Permission{
>   
> PApplet parent;
> String p;
> 
> public Permission(PApplet pParent,String permissionName) {
> p = permissionName;
> parent = pParent;
> parent.requestPermission("android.permission."+permissionName, "onPermissionResult", this);
> println(permissionName);
> };
> 
> public void onPermissionResult(boolean granted) {
> if (!granted) {
> PApplet.println("User did not grant",p," permission.",p,"is disabled.");
> }
> };
> 
> };
> 
> ```

```auto
f.setLocation("xml/test.xml");

```

 ![Screenshot_20210513_020001_com.mi.android.globalFileexplorer](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/ddf4b6f20eb3b5592fe88fffe4db264dcfd0ad0e.jpeg)

---

_[View the full topic](https://discourse.processing.org/t/no-serial-library-processing-for-smartphone-help/29870)._
