简体   繁体   English

使用 Unity 在 Android 上列出 SD 卡上的 txt 文件

[英]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文件。

  • permissions READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE are given授予READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE权限
  • the txt files exist, I can see them using my file browser on Android. txt 文件存在,我可以在 Android 上使用我的文件浏览器查看它们。 I can also see them in Windows when I connect my phone当我连接手机时,我也可以在 Windows 中看到它们

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.jpgbla.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:问题:

  • Why are the txt files not listed by Unity's C# API?为什么 Unity 的 C# API 没有列出 txt 文件?
  • How do I get access to the txt files?如何访问 txt 文件?
  • Maybe the app requires more permissions?也许该应用程序需要更多权限?
  • Is it possible to ask Android for full access to a specific folder on an SD card?是否可以要求 Android 完全访问 SD 卡上的特定文件夹?
    • Some of my Android apps show a dialog to select a folder on an SD card before accessing stuff.我的一些 Android 应用程序会显示一个对话框,用于在访问内容之前选择 SD 卡上的文件夹。 Maybe this is required?也许这是必需的?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM