saveStrings("/sdcard/" + "basher.csv" , text) not working on Android?

Why saveStrings("/sdcard/" + “basher.csv” , text) not working on android ?

@basher24===
put your code & error code.
What is supposed to be this “/sdcard/” string?

@basher24 Is this a Processing for Android issue? Also, can you further explain your issue? please!

void Save_function(int file)
{
   
Table table;  
  
try {
   
  
    table =   loadTable("/sdcard/" + str(file) +  ".csv");   // trying to load a file from samsung SD CARD 
     
    TableRow newRow = table.addRow();
    
     newRow.setString(0,"LIVE"+file) ;
     newRow.setString(1,Username) ;
     newRow.setString(2,Site) ;
 
    for(int i=3,j=0;j<FieldNumber;i++,j++) {
         
    newRow.setString(i,textlistText[j]);
    
    }
    
    table.addRow(newRow);
    
    saveTable(table, "/sdcard/" + str(file) +  ".csv"); 
        
    }
   catch (Exception e) { 
     e.printStackTrace();
     table = new Table();
     saveTable(table, "/sdcard/"+ str(file) +  ".csv");   // If the  load a failed create new file at samsung SD CARD 
     
   }  
}

java.lang.IllegalArgumentException: File /sdcard/1.csv contains a path separator
at android.app.ContextImpl.makeFilename(ContextImpl.java:2395)
at android.app.ContextImpl.openFileInput(ContextImpl.java:504)
at android.content.ContextWrapper.openFileInput(ContextWrapper.java:193)
at processing.core.PSurfaceNone.openFileInput(Unknown Source)
at processing.core.PApplet.createInputRaw(Unknown Source)
at processing.core.PApplet.createInput(Unknown Source)
at processing.core.PApplet.loadTable(Unknown Source)
at processing.core.PApplet.loadTable(Unknown Source)
at processing.test.bb1.bb1.Save_function(bb1.java:749)
at processing.test.bb1.bb1.mousePressed(bb1.java:1410)
at processing.core.PApplet.handleMouseEvent(Unknown Source)
at processing.core.PApplet.dequeueEvents(Unknown Source)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PSurfaceNone.callDraw(Unknown Source)
at processing.core.PSurfaceNone$AnimationThread.run(Unknown Source)
java.io.FileNotFoundException: /sdcard/1.csv (Permission denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(FileOutputStream.java:221)
at java.io.FileOutputStream.(FileOutputStream.java:169)
at processing.core.PApplet.createOutput(Unknown Source)
at processing.data.Table.save(Unknown Source)
at processing.core.PApplet.saveTable(Unknown Source)
at processing.core.PApplet.saveTable(Unknown Source)
at processing.test.bb1.bb1.Save_function(bb1.java:771)
at processing.test.bb1.bb1.mousePressed(bb1.java:1410)
at processing.core.PApplet.handleMouseEvent(Unknown Source)
at processing.core.PApplet.dequeueEvents(Unknown Source)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PSurfaceNone.callDraw(Unknown Source)
at processing.core.PSurfaceNone$AnimationThread.run(Unknown Source)
FATAL EXCEPTION: Animation Thread
Process: processing.test.bb1, PID: 599
java.lang.NullPointerException: Attempt to invoke virtual method ‘void java.io.OutputStream.flush()’ on a null object reference
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:141)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:297)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
at java.io.PrintWriter.flush(PrintWriter.java:320)
at processing.data.Table.writeCSV(Unknown Source)
at processing.data.Table.save(Unknown Source)
at processing.data.Table.save(Unknown Source)
at processing.core.PApplet.saveTable(Unknown Source)
at processing.core.PApplet.saveTable(Unknown Source)
at processing.test.bb1.bb1.Save_function(bb1.java:771)
at processing.test.bb1.bb1.mousePressed(bb1.java:1410)
at processing.core.PApplet.handleMouseEvent(Unknown Source)
at processing.core.PApplet.dequeueEvents(Unknown Source)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PSurfaceNone.callDraw(Unknown Source)
at processing.core.PSurfaceNone$AnimationThread.run(Unknown Source)

Can you please tell us where did you place this file? Are you planning to modify this file in your code or will it be only used as a resource which is loaded but not modified?

If you are not modifying this file, then you should place the file inside a folder named data which must be located inside your sketch folder. Another way to add this file to your sketch is to drag the file from an explore window and drop it in the sketch window. The PDE will create the data folder for you and copy the file to that directory.

If you are loading this file from an external drive, as I can see from your attempt in your code, then you need to have a look at this post here. Please don’t forget to enable writing permission to the external storage. To do so, in the PDE in Android mode, go to Sketch>>Permissions and then scroll down to almost the end of the list and check the writing to external.

More links related to external storage in Android:

https://forum.processing.org/two/discussion/18126/load-image-files-from-external-storage-to-pimage/p1 https://forum.processing.org/two/discussion/9893/save-images-in-specific-folders-android#latest https://forum.processing.org/two/discussion/comment/77652/#Comment_77652

Don’t forget to check the Android documentation as the Android API offers other options that might be suitable to your task:

https://developer.android.com/guide/topics/data/
https://developer.android.com/training/data-storage/files#WriteExternalStorage

Kf

1 Like