Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

I can modify a file in external storage, but cannot create it. Why is that ?

Discussion in 'Android' started by ArnoBen, Jun 7, 2019.

  1. ArnoBen

    ArnoBen

    Joined:
    Apr 24, 2019
    Posts:
    13
    Greetings everyone,

    I need to log some stuff in a csv and make it easily accessible on the phone storage, no big deal. I work on an Oculus Go.
    I granted ExternalStorageWrite permissions. Now here is what happens :
    I have a script that writes a file MyLog.csv in "mnt/sdcard/Download/". The thing is, if this file does exist, it works, BUT if the file doesn't exist, it doesn't create it. BUT it does create it when I debug on my computer ! Which means it's an android-related issue. I've been searching a lot on forums & stuff but to no avail.
    Is this a limitation or is there a workaround ?

    Thanks.
     
  2. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    186
  3. ArnoBen

    ArnoBen

    Joined:
    Apr 24, 2019
    Posts:
    13
    Hi, thank you for your answer.
    I read some docs and found this function that seems to be wildly used to access external storage : http://anja-haumann.de/unity-how-to-save-on-sd-card/
    However, this raises the following error :

    DirectoryNotFoundException: Could not find a part of the path "/storage/emulated/0/Android/data/com.Company.Name/files/Download/WordsLog.csv".

    I'm not trying to access the Android/data folder, and I'm not sure this is entirely relevant.
    As I stated, the script manages to modify a csv when it already exists at the path "mnt/sdcard/Download/", which means it must be the correct path, otherwise it couldn't modify it, could it ? I can be wrong of course, this whole situation is a bit frustrating since this sound like an obvious "feature".
    Thanks again for any help.
     
  4. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    186
    Try using Application.temporaryCachePath instead. The folder permissions for the cache path is lax. And contrary to the concept of 'temporary', the cache does not get deleted automatically until users specifically choose to via settings.
     
  5. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    186
    FYI, the cache directory is also where sqlite writes its db files to anyways.
     
  6. ArnoBen

    ArnoBen

    Joined:
    Apr 24, 2019
    Posts:
    13
    For the future suffering souls stumbling onto my post : the solution was to reboot the Oculus Go after saving the files. As stated here, it's a side effect of the MTP protocol which only shows files and folders it has "knowledge of".
    Moreover, "mnt/sdcard/Download" or "mnt/sdcard/Movies" etc. are the correct paths to access files in these repertories that we can see when pluggin the headset on a computer.