Search Unity

[Android] File.Open not work

Discussion in 'Scripting' started by TrueNoob141, Sep 20, 2019.

  1. TrueNoob141

    TrueNoob141

    Joined:
    Sep 18, 2019
    Posts:
    30
    Why code didn't work in android build (not editor)?

    Code (CSharp):
    1. using System.IO;
    2. using System.Text;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.   private void Start()
    8.   {
    9.     string path = Path.Combine(Application.streamingAssetsPath, "file.txt");
    10.     Debug.Log("Path " + path);
    11.  
    12.     FileStream stream;
    13.     try
    14.     {
    15.       stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    16.     }
    17.     catch (System.Exception ex)
    18.     {
    19.       Debug.LogError("Exception " + ex.Message);
    20.       Debug.LogException(ex);
    21.       throw ex;
    22.     }
    23.  
    24.     Debug.Log("Opened!");
    25.    
    26.     using (var reader = new StreamReader(stream, Encoding.UTF8))
    27.     {
    28.       string result = reader.ReadToEnd();
    29.       Debug.Log(result);
    30.     }
    31.   }
    32. }
    33.  
    file.txt placed in StreamingAssets with text "Hello, world"

    Code (csharp):
    1.  
    2. Unity Output:
    3. Path jar:file:///data/app/com.tn141.test-k9eUQRri9cUpPw2m2Pid3Q==/base.apk!/assets/file.txt
    4. Exception Could not find a part of the path "/jar:file:/data/app/com.tn141.test-k9eUQRri9cUpPw2m2Pid3Q==/base.apk!/assets/file.txt".
    5.  
    6. Logcat output
    7. 09-20 02:33:30.011 14847 14865 I Unity   : Path jar:file:///data/app/com.tn141.test-k9eUQRri9cUpPw2m2Pid3Q==/base.apk!/assets/file.txt
    8. 09-20 02:33:30.011 14847 14865 I Unity   : UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
    9. 09-20 02:33:30.011 14847 14865 I Unity   : UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
    10. 09-20 02:33:30.011 14847 14865 I Unity   : UnityEngine.Logger:Log(LogType, Object)
    11. 09-20 02:33:30.011 14847 14865 I Unity   : UnityEngine.Debug:Log(Object)
    12. 09-20 02:33:30.011 14847 14865 I Unity   : Test:Start() (at D:\Projects\bug_android_file\Assets\Test.cs:12)
    13. 09-20 02:33:30.011 14847 14865 I Unity   :
    14. 09-20 02:33:30.011 14847 14865 I Unity   : (Filename: D Line: 0)
    15. 09-20 02:33:30.011 14847 14865 I Unity   :
    16. 09-20 02:33:30.026 14847 14865 E Unity   : Exception Could not find a part of the path "/jar:file:/data/app/com.tn141.test-k9eUQRri9cUpPw2m2Pid3Q==/base.apk!/assets/file.txt".
    17. 09-20 02:33:30.026 14847 14865 E Unity   : UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
    18. 09-20 02:33:30.026 14847 14865 E Unity   : UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
    19. 09-20 02:33:30.026 14847 14865 E Unity   : UnityEngine.Logger:Log(LogType, Object)
    20. 09-20 02:33:30.026 14847 14865 E Unity   : UnityEngine.Debug:LogError(Object)
    21. 09-20 02:33:30.026 14847 14865 E Unity   : Test:Start() (at D:\Projects\bug_android_file\Assets\Test.cs:21)
    22. 09-20 02:33:30.026 14847 14865 E Unity   :
    23. 09-20 02:33:30.026 14847 14865 E Unity   : (Filename: D Line: 0)
    24. 09-20 02:33:30.026 14847 14865 E Unity   :
    25. 09-20 02:33:30.055 14847 14865 E Unity   : DirectoryNotFoundException: Could not find a part of the path "/jar:file:/data/app/com.tn141.test-k9eUQRri9cUpPw2m2Pid3Q==/base.apk!/assets/file.txt".
    26. 09-20 02:33:30.055 14847 14865 E Unity   :   at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0017d] in <c6bd535f6ab848b4a13f34d01b756eef>:0
    27. 09-20 02:33:30.055 14847 14865 E Unity   :   at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <c6bd535f6ab848b4a13f34d01b756eef>:0
    28. 09-20 02:33:30.055 14847 14865 E Unity   :   at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
    29. 09-20 02:33:30.055 14847 14865 E Unity   :   at System.IO.File.Open (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <c6bd535f6ab848b4a13f34d01b756eef>:0
    30. 09-20 02:33:30.055 14847 14865 E Unity   :   at Test.Start () [0x00023] in D:\Projects\bug_android_file\Assets\Test.cs:17
    31. 09-20 02:33:30.055 14847 14865 E Unity   : UnityEngine.DebugLogHandl
    32. 09-20 02:33:30.056 14847 14865 E Unity   : DirectoryNotFoundException: Could not find a part of the path "/jar:file:/data/app/com.tn141.test-k9eUQRri9cUpPw2m2Pid3Q==/base.apk!/assets/file.txt".
    33. 09-20 02:33:30.056 14847 14865 E Unity   :   at Test.Start () [0x0004f] in D:\Projects\bug_android_file\Assets\Test.cs:23
    34. 09-20 02:33:30.056 14847 14865 E Unity   :
    35. 09-20 02:33:30.056 14847 14865 E Unity   : (Filename: D Line: 0)
    36. 09-20 02:33:30.056 14847 14865 E Unity   :
    37.  
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    From the documentation for streamingassets :
    Android uses files inside a compressed APK
    /JAR file,
    "jar:file://" + Application.dataPath + "!/assets".

    You probably need to 'download' those files with Www or UnityWebRequest rather than using File.Open