Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TreasureKey · Nov 05, 2013 at 06:45 AM · androidassetspathstreamingfolder

Does DirectoryInfo GetFiles work on Android ?

I need to list up all png files in a sub folder of the assets folder in my exported android project.

I have verified that the sub folder is in the assets folder of my exported android project. It has some .png files in it.

However, I keep getting the error: DirectoryNotFoundException .

Does DirectoryInfo GetFiles work on android ?

Sample Code:

string streamingAssetsPathAndroid = "jar:file://" + Application.dataPath + "!/assets/";

string subFolderPath = "Some/Other/" + "folder";

string fullpath = streamingAssetsPathAndroid + subFolderPath;

DirectoryInfo dirInfo = new DirectoryInfo(fullpath);

FileInfo[] fileInfo = dir.GetFiles("*.png");

Result:

DirectoryNotFoundException: Directory 'jar:file:/data/app/com.your.company.game.apk!/assets/Some/Other/folder' not found.

I have tried various other paths like:

string streamingAssetsPathAndroid = "jar:file://" + Application.dataPath + "!/assets/";

Or

string streamingAssetsPathAndroid = "file://" + Application.dataPath + "!/assets/";

Or

string streamingAssetsPathAndroid = "Application.dataPath + "!/assets/";

Or

string streamingAssetsPathAndroid = "assets/";

and then combining it with the sub folder path. But I get a DirectoryNotFoundException error every time.

Please let me know if there is a proper/recommended way to do this.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · Nov 05, 2013 at 06:47 AM

Well resources just in Assets aren't going to be in your build, they get packed up and all sorts, and then only if they are used. Assets in the Streaming Assets folder will be copied into the jar for your build.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreasureKey · Nov 05, 2013 at 08:04 AM 0
Share

The sub folders and pngs are in my strea$$anonymous$$g assets folder in unity. When it gets exported as a google android project, they are in the android project's assets folder.

Is there some (alternative) way to list up all .png files in my sub folder ?

avatar image Bunny83 · Nov 05, 2013 at 08:31 AM 1
Share

Hmm, is there a reason you don't use Application.strea$$anonymous$$gAssetsPath like suggested on this page?

Also keep in $$anonymous$$d, like mentioned on the doc page, on android the files are not in a folder, they are within a jar file. So GetFiles won't work on this path since it's not an actual path. If you want to "browse" your folder you need a zip-library to open the jar manually.

GetFiles works perfectly well on real folders as long as you have file permission.

avatar image TreasureKey · Nov 05, 2013 at 08:50 AM 0
Share

I actually have tried using: string strea$$anonymous$$gAssetsPathAndroid = Application.strea$$anonymous$$gAssetsPath;

Which pretty much results in the same path string and the same folder not found error for me.

I may have to consider that it may not be possible to use Directory.GetFiles on a unity game exported to google android project...

avatar image whydoidoit · Nov 05, 2013 at 08:53 AM 0
Share

Bunny's right you can't scan the directory because it is in a ZIP file. You can use a ZIP library to exa$$anonymous$$e the JAR and get files from it.

avatar image whydoidoit · Nov 05, 2013 at 08:54 AM 0
Share

I use this: http://www.icsharpcode.net/opensource/sharpziplib/ for a lot of stuff.

Show more comments
avatar image
0

Answer by shadyshrif · Jul 04, 2017 at 07:45 PM

To solve this problem I created pre-compilation script which will run before the compilation; this script will list the names of the files you want to access later in text file

 #if UNITY_EDITOR
 using UnityEditor.Build;
 using UnityEditor;
 using System.IO;
 
 public class BM_AndroidBuildPrepartion : IPreprocessBuild
 {
     public int callbackOrder { get { return 0; } }
     public void OnPreprocessBuild(BuildTarget target, string path)
     {
         // Do the preprocessing here
         string[] fileEntries = Directory.GetFiles("Assets/Resources/Prefabs/alphabet", "*.prefab");
         System.IO.Directory.CreateDirectory("Assets/StreamingAssets/");
         using (StreamWriter sw = new StreamWriter("Assets/StreamingAssets/alphabet.txt", false))
         {
           
         foreach (string filename in fileEntries) {
                 sw.WriteLine(Path.GetFileNameWithoutExtension(filename));
         }
           
         }
     }
 }
 #endif

then I read the text file and you can access the files in same way but you have to put them inside your project in Assets/StreamingAssets/

   #if UNITY_ANDROID
        string  path = "jar:file://" + Application.dataPath + "!/assets/alphabet.txt";
          WWW wwwfile = new WWW(path);
          while (!wwwfile.isDone) { }
          var filepath = string.Format("{0}/{1}", Application.persistentDataPath, "alphabet.t");
          File.WriteAllBytes(filepath, wwwfile.bytes);
  
          StreamReader wr = new StreamReader(filepath);
              string line;
              while ((line = wr.ReadLine()) != null)
              {
              //your code
              }
   #endif

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to write files to the streaming asset folder in Android at runtime 1 Answer

How to find a path to a folder in the Assets folder? 1 Answer

can't load music (wav and mp3 ) on android but the code works fine in editor and window build 0 Answers

Where can i find the Swords and Shovels Game Design Document? 3 Answers

insert my folder into an apk 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges