What happened???
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
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
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
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.
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?
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.