saveStrings + loadStrings problem

Hi,
I want to save and load a String in my programm.
I could use:

saveStrings("filename.txt", new String[]{string});
loadStrings("filename.txt");

but the problem with these functions is (as far as I know) that they save the file at:

/data/user/0/com.yourCompany.yourAppName/files/filename.txt

The user cant open this Path. But in my case the User have to be able to open this path.
For example a good save location would be:

/storage/emulated/0/MYAPP/filename.txt
//or
/storage/emulated/0/Android/data/com.yourCompany.yourAppName/files/filename.txt

So I want to create a custom saveStings function.
Here is my attempt but its not working. can someone tell my why?
Thanks in advance :slight_smile:

import android.os.Environment;
import android.content.Context;
import java.io.FileOutputStream;

final String APP_FOLDER = "MYAPP";

void setup(){
  fullScreen();
  requestPermission("android.permission.WRITE_EXTERNAL_STORAGE");
  requestPermission("android.permission.READ_EXTERNAL_STORAGE");
  saveString("test", "text.txt");
}

void saveString(String text, String fileName) {
  try {
    File f = new File(Environment.getExternalStorageDirectory(), APP_FOLDER);
    if (!f.exists()) {
      println("create dirs");
      f.mkdirs();
    }
    println("Path: " + f.getAbsolutePath());
    File file = new File(f, fileName);
    println("File exists: " + file.exists());
    FileOutputStream fos = new FileOutputStream(fileName, true); // save
    fos.write(text.getBytes());
    fos.close();
  }
  catch(Exception e) {
    e.printStackTrace();
  }
}

If the cause
‘android not saving Permission denied’
If you say
android:requestLegacyExternalStorage="true"'
Please add

1 Like

@GWAK thanks for replying,
Where should I add this? android:requestLegacyExternalStorage=“true”’

This is the output:
My println output

create dirs
Path: /storage/emulated/0/MYAPP

Error Message

java.io.FileNotFoundException: text.txt: open failed: EROFS (Read-only file system)
	at libcore.io.IoBridge.open(IoBridge.java:492)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:157)
	at com.flologames.savestrings.SaveStringsOnAndroid.saveString(SaveStringsOnAndroid.java:51)
	at com.flologames.savestrings.SaveStringsOnAndroid.setup(SaveStringsOnAndroid.java:33)
	at processing.core.PApplet.handleDraw(PApplet.java:1878)
	at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:478)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:518)
Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
	at libcore.io.Linux.open(Native Method)
	at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
	at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
	at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
	at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8405)
	at libcore.io.IoBridge.open(IoBridge.java:478)
	... 7 more

1 Like

@Flolo

  1. file : AndroidManifest.xml

  2. copy

android:label="" android:requestLegacyExternalStorage=“true”

If it’s a permission issue
just add that

1 Like

Hi
I get the same error as before…
my 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="com.flologames.savestrings">
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="29"/>
    <application android:icon="@mipmap/ic_launcher" android:label="" android:requestLegacyExternalStorage="true">
        <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.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

1 Like

link : Android-java-code-utilities-widgets-for-Processing-for-Android/saveStrings().txt at master · EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android · GitHub

Did you see what ‘EmmanuelPil’ wrote?

1 Like