I want to list all txt
files in a folder on an SD card on Android using the Unity game engine.
- permissions
READ_EXTERNAL_STORAGE
andWRITE_EXTERNAL_STORAGE
are given - the txt files exist, I can see them using my file browser on Android. I can also see them in Windows when I connect my phone
But Unity's C# API is not listing any txt files in the folder:
// sdCardFolder is for example "/storage/9C33-6BBD/MyFolderOnSdCard"
string[] filePaths = Directory.GetFiles(sdCardFolder, "*", SearchOption.AllDirectories);
filePaths.ForEach(filePath => Debug.Log(filePath));
But it shows only some of the files. It seems to list images (jpg, png), audio files (mp3, ogg), video files (avi), and maybe more. But it does not list any txt file.
I renamed a file from bla.txt
to bla.jpg
and bla.foo
. The jpg file was listed, but bla.foo
was not. Thus, I assume that Android by default only provides access to "typical" media and application files.
Question:
- Why are the txt files not listed by Unity's C# API?
- How do I get access to the txt files?
- Maybe the app requires more permissions?
- Is it possible to ask Android for full access to a specific folder on an SD card?
- Some of my Android apps show a dialog to select a folder on an SD card before accessing stuff. Maybe this is required?
I am testing with Android 12, Unity 2021.3.4f1.
Motivation
I have a karaoke game where users can add their own songs. The song format uses txt (plain text) files. I want to enable users to put their song library on an SD card.