[英]List txt files on SD card on Android with Unity
I want to list all txt
files in a folder on an SD card on Android using the Unity game engine.我想使用 Unity 游戏引擎在 Android 上的 SD 卡上的文件夹中列出所有
txt
文件。
READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
are givenREAD_EXTERNAL_STORAGE
和WRITE_EXTERNAL_STORAGE
权限But Unity's C# API is not listing any txt files in the folder:但是 Unity 的 C# API 没有列出文件夹中的任何 txt 文件:
// 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.
它似乎列出了图像(jpg、png)、音频文件(mp3、ogg)、视频文件(avi)等等。 But it does not list any txt file.
但它没有列出任何 txt 文件。
I renamed a file from bla.txt
to bla.jpg
and bla.foo
.我将一个文件从
bla.txt
重命名为bla.jpg
和bla.foo
。 The jpg file was listed, but bla.foo
was not. jpg 文件已列出,但
bla.foo
未列出。 Thus, I assume that Android by default only provides access to "typical" media and application files.因此,我假设 Android 默认只提供对“典型”媒体和应用程序文件的访问。
Question:问题:
I am testing with Android 12, Unity 2021.3.4f1.我正在使用 Android 12 Unity 2021.3.4f1 进行测试。
Motivation动机
I have a karaoke game where users can add their own songs.我有一个卡拉 OK 游戏,用户可以在其中添加自己的歌曲。 The song format uses txt (plain text) files.
歌曲格式使用 txt(纯文本)文件。 I want to enable users to put their song library on an SD card.
我想让用户将他们的歌曲库放在 SD 卡上。
Android indeed seems to limit the visiblity of files. Android 似乎确实限制了文件的可见性。 But not inside folders that belong to the app itself.
但不在属于应用程序本身的文件夹内。 You can find these folders via
getExternalFilesDirs
on Android.您可以通过 Android 上的
getExternalFilesDirs
找到这些文件夹。
Here is an explanation how to call getExternalFilesDirs in Unity: http://anja-haumann.de/unity-how-to-save-on-sd-card/以下是如何在 Unity 中调用 getExternalFilesDirs 的说明: http ://anja-haumann.de/unity-how-to-save-on-sd-card/
private static string GetAndroidExternalFilesDir()
{
using (AndroidJavaClass unityPlayer =
new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject context =
unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
// Get all available external file directories (emulated and sdCards)
AndroidJavaObject[] externalFilesDirectories =
context.Call<AndroidJavaObject[]>
("getExternalFilesDirs", (object)null);
AndroidJavaObject emulated = null;
AndroidJavaObject sdCard = null;
for (int i = 0; i < externalFilesDirectories.Length; i++)
{
AndroidJavaObject directory = externalFilesDirectories[i];
using (AndroidJavaClass environment =
new AndroidJavaClass("android.os.Environment"))
{
// Check which one is the emulated and which the sdCard.
bool isRemovable = environment.CallStatic<bool>
("isExternalStorageRemovable", directory);
bool isEmulated = environment.CallStatic<bool>
("isExternalStorageEmulated", directory);
if (isEmulated)
emulated = directory;
else if (isRemovable && isEmulated == false)
sdCard = directory;
}
}
// Return the sdCard if available
if (sdCard != null)
return sdCard.Call<string>("getAbsolutePath");
else
return emulated.Call<string>("getAbsolutePath");
}
}
}
Note that READ_EXTERNAL_STORAGE permission is still required.请注意,仍然需要 READ_EXTERNAL_STORAGE 权限。
I found that in the folders returned by getExternalFilesDirs
, all files are visible to Unity's C# API.我发现在
getExternalFilesDirs
返回的文件夹中,所有文件都对 Unity 的 C# API 可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.