Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dn't write on android 14 #100

Open
AP2020C opened this issue Sep 7, 2024 · 10 comments
Open

Dn't write on android 14 #100

AP2020C opened this issue Sep 7, 2024 · 10 comments

Comments

@AP2020C
Copy link

AP2020C commented Sep 7, 2024

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












`
@yasirkula
Copy link
Owner

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

@AP2020C
Copy link
Author

AP2020C commented Sep 11, 2024

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

@yasirkula
Copy link
Owner

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" );

@AP2020C
Copy link
Author

AP2020C commented Sep 12, 2024

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.

@yasirkula
Copy link
Owner

May I see your final code?

@AP2020C
Copy link
Author

AP2020C commented Sep 13, 2024

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

https://pastebin.com/KCbWteAt

@yasirkula
Copy link
Owner

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.

@AP2020C
Copy link
Author

AP2020C commented Sep 13, 2024

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

@yasirkula
Copy link
Owner

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.

@AP2020C
Copy link
Author

AP2020C commented Sep 14, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants