Tai Wu
Tai Wu

Reputation: 83

In Unity, GetDirectories() does not work on Android

I've been trying to retrieve directories within Resources folder (Assets -> Resources -> DiscoveryPage -> Bloggers -> various folders I want to get), and it only works on Unity Editor on run time, but not on my Android device.

Here's what I have:

if (Application.platform == RuntimePlatform.Android) {
        //discoveryDir = new DirectoryInfo(Application.dataPath + "/Resources/DiscoveryPage/Bloggers");
        //discoveryDir = new DirectoryInfo(Application.persistentDataPath + "/Resources/DiscoveryPage/Bloggers");
        discoveryDir = new DirectoryInfo("jar:file://" + Application.dataPath + "!/assets" + "/Resources/DiscoveryPage/Bloggers");

        MainUI.ShowDebug("Running on Android. @" + discoveryDir.Name);
} else {
        discoveryDir = new DirectoryInfo(Application.dataPath + "/Resources/DiscoveryPage/Bloggers");
}

    bloggerDirs = discoveryDir.GetDirectories();
    if (bloggerDirs == null)
        MainUI.ShowDebug("bloggerDirs is null");
    else
        MainUI.ShowDebug("bloggerDirs is NOT null");

The line:

MainUI.ShowDebug("Running on Android. @" + discoveryDir.Name);

did show up with proper information (I was able to reach Bloggers folder and get the discoveryDir.Name, but

if (bloggerDirs == null)
 MainUI.ShowDebug("bloggerDirs is null");
else
 MainUI.ShowDebug("bloggerDirs is NOT null");

bloggerDirs doesn't return anything (not even null). Neither debug line was printed out.

Seems like discoveryDir.GetDirectories(); does not work on Android.

Anyone knows the problem and how to get those directories on Android run time?

Upvotes: 1

Views: 727

Answers (1)

tatmanblue
tatmanblue

Reputation: 1227

I know this is a very old question. I am putting here in case it helps someone else as I have not see any responses which indicate the issue may be related to permissions, as it turned out being the issue in my case. I stumbled on this as I was researching this exact issue. Please see this article for manifest permissions such as READ_MEDIA_AUDIO which is new with API 33.

Upvotes: 0

Related Questions