# saveStrings + loadStrings problem

**URL:** <https://discourse.processing.org/t/savestrings-loadstrings-problem/35289>\
**Category:** Processing for Android\
**Created:** [February 19, 2022, 12:27pm UTC](https://discourse.processing.org/t/savestrings-loadstrings-problem/35289 "2022-02-19T12:27:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [February 19, 2022, 12:27pm UTC](https://discourse.processing.org/t/savestrings-loadstrings-problem/35289/1 "2022-02-19T12:27:46Z")

</div>

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

```auto
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:

```auto
/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:

```auto
/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 🙂

```auto
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();
  }
}

```

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [February 19, 2022, 1:48pm UTC](https://discourse.processing.org/t/savestrings-loadstrings-problem/35289/2 "2022-02-19T13:48:20Z")

</div>

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

> <https://stackoverflow.com/questions/63364476/requestlegacyexternalstorage-is-not-working-in-android-11-api-30>

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [February 19, 2022, 2:38pm UTC](https://discourse.processing.org/t/savestrings-loadstrings-problem/35289/3 "2022-02-19T14:38:00Z")

</div>

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

This is the output:  
My println output

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

```

Error Message

```auto
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

```

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [February 19, 2022, 4:06pm UTC](https://discourse.processing.org/t/savestrings-loadstrings-problem/35289/4 "2022-02-19T16:06:19Z")

</div>

@Flolo

1. file : AndroidManifest.xml

2. copy

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

If it’s a permission issue  
just add that

 ![fig](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8acbc3ed69a36b6f3167175355239accd76fdff6.jpeg)

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [February 19, 2022, 4:19pm UTC](https://discourse.processing.org/t/savestrings-loadstrings-problem/35289/5 "2022-02-19T16:19:58Z")

</div>

Hi  
I get the same error as before…  
my AndroidManifest.xml:

```auto
<?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>

```

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [February 19, 2022, 4:36pm UTC](https://discourse.processing.org/t/savestrings-loadstrings-problem/35289/6 "2022-02-19T16:36:26Z")

</div>

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](https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android/blob/master/saveStrings().txt)

Did you see what ‘EmmanuelPil’ wrote?
