Unable to write files, not even in the editor! 🤬

What happened??? :face_with_symbols_over_mouth::face_with_symbols_over_mouth::face_with_symbols_over_mouth::face_with_symbols_over_mouth::face_with_symbols_over_mouth::face_with_symbols_over_mouth::face_with_symbols_over_mouth::face_with_symbols_over_mouth::face_with_symbols_over_mouth::face_with_symbols_over_mouth:
For several days now, all the instructions for writing files I have tried have given an error.
I created a very simple program: just a GO with this script attached

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Myclass: MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
        Debug.Log(GetAndroidExternalStoragePath());
        File.WriteAllText(GetAndroidExternalStoragePath(),"MyText");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private string GetAndroidExternalStoragePath()
    {
        if (Application.platform != RuntimePlatform.Android)
            return Application.persistentDataPath;

        var jc = new AndroidJavaClass("android.os.Environment");
        var path = jc.CallStatic<AndroidJavaObject>("getExternalStoragePublicDirectory",
            jc.GetStatic<string>("DIRECTORY_DCIM"))
            .Call<string>("getAbsolutePath");
        return path;
    }
}

Yet even running it in the editor, so without even getting to the android compilation, I get

UnauthorizedAccessException: Access to the path ‘C:\Users\me\AppData\LocalLow\DefaultCompany\write test’ is denied.
System.IO.FileStream…ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <8ce0bd04a7a04b4b9395538239d3fdd8>:0)
System.IO.FileStream…ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options) (at <8ce0bd04a7a04b4b9395538239d3fdd8>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream…ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions)
System.IO.StreamWriter…ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize) (at <8ce0bd04a7a04b4b9395538239d3fdd8>:0)
System.IO.StreamWriter…ctor (System.String path) (at <8ce0bd04a7a04b4b9395538239d3fdd8>:0)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter…ctor(string)
System.IO.File.WriteAllText (System.String path, System.String contents) (at <8ce0bd04a7a04b4b9395538239d3fdd8>:0)

Don’t tell me to check the write permissions, because of course I have them!

I am a little puzled

from your code, if running under windows/linux/mac would return the Application.persistentDataPath, have not added a file name to the path, therefore it cant create a no named file… This sounds like your issue rather than permissions

You’re right, that’s what happens when Unity starts generating nonsense error on your main project.

Now I tried following Managing External Files in Unity: A Step-by-Step Guide, page updated just 2 months ago, in the editor it works, it just doesn’t create any files on android.

I set the write permission to external and added to the manifest.

Well, I tried to visit it without an adblocker and realised that it is a bad page. And the contacts do not exist

Sadly I dont tend to do much with android, i mean i got a cheap tablet and made a tower defense to play on it, but nothing more… no files needed, in fact i didnt even save the score :stuck_out_tongue:

However, Id expect unity to honour the Application.persistentDataPath for android too, so a merge path while filename, and writealltext should then produce a file.

// Start is called before the first frame update
void Start()
{
    try
    {
        string p = GetAndroidExternalStoragePath();

        Directory.CreateDirectory(p);


        
        Debug.Log(p);
        File.WriteAllText(Path.Combine(p, "n.log"), p + "\n" + System.DateTime.Now.ToString());


        Log = new StreamWriter(Path.Combine(p, "l.txt"), false);

        wLog("gklhuilrgyilsrg");
        wLog("u68678i578i");
        wLog("mvmcvgnv");

        Log.Close();
        txt.text = "f";
    }
    catch(Exception e)
    {
        txt.text = e.ToString();
    }
}

public void wLog(string t)
{
    Log.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + t);
    Log.Flush();
}

// Update is called once per frame
void Update()
{
    
}
private string GetAndroidExternalStoragePath()
{
    if (Application.platform != RuntimePlatform.Android)return Application.persistentDataPath;

    
    var jc = new AndroidJavaClass("android.os.Environment");
    var path = jc.CallStatic<AndroidJavaObject>("getExternalStoragePublicDirectory",
        jc.GetStatic<string>("DIRECTORY_DOCUMENTS"))
        .Call<string>("getAbsolutePath");

    path = Path.Combine(path, "me", "test"); 

    return path;
}

works on android 6.
on android 14 instead it creates folders but not files

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    xmlns:tools="http://schemas.android.com/tools">
    <application>
        <activity android:name="com.unity3d.player.UnityPlayerActivity"
                  android:theme="@style/UnityThemeSelector">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

</manifest>

have you debugged the path it creates? If you make a path with /me/test at the end, have you tried ensuring that the folder me is made first, and then making the file in it?

The error is just because it’s an invalid path. You probably need to include a file extension.

Yes, the path is correct. On android 14 it creates the correct folder path, but does not create the file.
On android 6, on the other hand, it works correctly.

Again, probably because there’s no extension. The editor error, which is coming from the Microsoft side of things is because it thinks you’re trying to write data to a folder, which will result in the error you’re getting.