UnitySimpleFileBrowser icon indicating copy to clipboard operation
UnitySimpleFileBrowser copied to clipboard

Dn't write on android 14

Open AP2020C opened this issue 1 year ago • 10 comments
trafficstars

Description of the bug

Assuming that all I want to file is to write a log file in a subfolder of the home or document folder, regardless of the device, thus without letting the user choose where to save, on android 14 it created a file in the place I selected but did not write the content Reproduction steps

`using SimpleFileBrowser; using System.Collections; using System.IO; using UnityEngine;

public class SaveFileExample : MonoBehaviour { private void Start() { SaveFile(); }

public void SaveFile()
{
    StartCoroutine(ShowSaveDialog());
}

private IEnumerator ShowSaveDialog()
{
    // open dialog
    yield return FileBrowser.WaitForSaveDialog(FileBrowser.PickMode.Files, false, System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile), "new file.txt", "Save", "save");

    // 
    if (FileBrowser.Success)
    {
        string filePath = FileBrowser.Result[0];

        // 
        using (StreamWriter writer = new StreamWriter(filePath, false))
        {
            writer.Write("Content");
        }

    }
}

} ` Attach this to any object. The file is created but not written Platform specs

Please provide the following info if this is a Unity 3D repository.

  • Unity version: 2022.3.45f1
  • Platform: Android (bad works), Windows (in editor, good work)
  • Device: Samsung Galaxy S8+, Android 14
  • How did you download the plugin: Asset Store

Additional info impossible to catch the error

manifest: `

`

AP2020C avatar Sep 07 '24 14:09 AP2020C

You need to use FileBrowserHelpers to work with Storage Access Framework file paths on Android.

yasirkula avatar Sep 07 '24 14:09 yasirkula

Can you share a code that shows how to do it?

AP2020C avatar Sep 11 '24 18:09 AP2020C

Example code uses FileBrowserHelpers.CopyFile( filePath, destinationPath ); and I'd recommend the same but you can also call other functions like FileBrowserHelpers.WriteTextToFile( filePath, "Hello World" );

yasirkula avatar Sep 11 '24 19:09 yasirkula

This time it didn't even create the file. I need to write the file in multiple times, so copying is not an applicable way.

AP2020C avatar Sep 12 '24 19:09 AP2020C

May I see your final code?

yasirkula avatar Sep 13 '24 08:09 yasirkula

I paste in an external file because it interprets it wrong here

https://pastebin.com/KCbWteAt

AP2020C avatar Sep 13 '24 14:09 AP2020C

If you'll be passing raw filepaths to FileBrowserHelpers, then you can just use System.IO. It won't work though because of Android's IO restrictions. Which is why you need to use FileBrowser to pick a filepath and use that path in FileBrowserHelpers. Paths returned by FileBrowser won't be raw filepaths but rather Storage Access Framework paths.

yasirkula avatar Sep 13 '24 15:09 yasirkula

Thanks, I don't know by what sorcery, it works, at least for now. I had come to the conclusion that android had banned writing files to external storage.

So it is not possible to export a file to a predetermined location, just asking the user for write permission only first time? Like documents/ap.2020/today.txt

I would like to ask you to add public functions to transform from an enum the standard paths, such as home, documents, sd, others, that can then be combined with subpaths

AP2020C avatar Sep 13 '24 18:09 AP2020C

WRITE_EXTERNAL_STORAGE is removed on latest Android versions so Storage Access Framework is mandatory going forward. persistentDataPath and temporaryCachePath are safe to use, though.

There isn't a function that takes a raw filepath and converts it to Storage Access Framework path. If you find a working solution, do let me know.

yasirkula avatar Sep 13 '24 19:09 yasirkula

OK, I was hoping that was possible. Thanks for the help. I hope someone will find a better solution.

AP2020C avatar Sep 14 '24 09:09 AP2020C