Search Unity

Writing to and reading from Application.persistentDataPath not working

Discussion in 'Android' started by SavedByZero, Jun 10, 2021.

  1. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    71
    I have this code:
    Code (CSharp):
    1. IEnumerator TransferFile(string fileName)
    2.     {
    3.         yield return new WaitForSeconds(0.25f);
    4.      
    5.         string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);
    6.         string result = "";
    7.         if (filePath.Contains("://") || filePath.Contains(":///"))
    8.         {
    9.             UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(filePath);
    10.             yield return www.SendWebRequest();
    11.             result = www.downloadHandler.text;
    12.         }
    13.         else
    14.             result = System.IO.File.ReadAllText(filePath);
    15.         File.WriteAllText(Application.persistentDataPath + "/"+fileName, result);
    16.         Debug.Log("p data path: " + Application.persistentDataPath);
    17.         string cascadeFileName = (Application.persistentDataPath + "/" + fileName);
    18.         Debug.Log("looking for cascade file name: " + cascadeFileName);
    19. [...]
    And I could have sworn it was working at one point, and then it wasn't. I set all the necessary permissions on my android phone (OnePlus 6T) for this app -- camera, storage, location. But I'm still getting error message that the file wasn't found. Is there something in this code that would make it pull from an inconsistent location? The shared library I'm connecting to hasn't moved, nor has the file location (I'm pulling it from my StreamingAssets folder)
     
  2. kaarloew

    kaarloew

    Joined:
    Nov 1, 2018
    Posts:
    361
    What does adb logcat complain when you execute the code?
     
  3. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    71
    Right here:
    Code (CSharp):
    1. 2021-06-11 10:38:59.179 18226-18255/? I/Unity: looking for cascade file name: /storage/emulated/0/Android/data/com.n123.xxx.lite/files/haarcascade_frontalface_default.xml
    2.     UnityEngine.Logger:Log(LogType, Object)
    3.     <TransferFile>d__13:MoveNext()
    4.     UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
    5.  
    6.     (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
    7. 2021-06-11 10:38:59.181 573-573/? I/hwservicemanager: getTransport: Cannot find entry android.hardware.graphics.allocator@3.0::IAllocator/default in either framework or device manifest.
    8. 2021-06-11 10:38:59.181 18226-18257/? W/Gralloc3: allocator 3.x is not supported
    9. 2021-06-11 10:38:59.193 18226-18255/? I/Unity: c++ returning found: -1
    And when I open the phone, the file is...right there in the folder. Sigh.
    if anyone's curious, the c++ from my .so library of OpenCV that reads this is:
    Code (CSharp):
    1. extern "C" int Init(int& outCameraWidth, int& outCameraHeight, String cascadeFileName)
    2. {
    3.  
    4.     if (!_faceCascade.load(cascadeFileName)) //cascadeFileName is getting this passed to it: /storage/emulated/0/Android/data/com.n123.xxx.lite/files/haarcascade_frontalface_default.xml
    5.         return -1;  //it's getting to this polint
    6.  
    7.     _capture.open(0);
    8.  
    IF anyone who remembers c++ better than I do knows how to trace something to the console from there (cout doesn't do it), I'd love to hear it.
     
    Last edited: Jun 11, 2021
unityunity