Search Unity

Question Writing files to Oculus Quest

Discussion in 'VR' started by Wez01, Mar 17, 2021.

  1. Wez01

    Wez01

    Joined:
    Oct 28, 2017
    Posts:
    9
    Heya all,
    I'm trying to write a .csv file and folder to the Quest2 headset so I can access it on the storage of the headset. I've been trying several methods I've found online without success. There seems to be several ways of targeting the storage none of which I've managed success with.

    It might be permissions. Some articles have mentioned it should pop up a access dialogue for the user (but I think that may be for reading rather than writing) which my app definitely doesn't.
    Project Settings > Player > Other > Write Permissions: External (SDCard)
    The UnityManifest file has: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    Here is the core bits of code I've tried
    Code (CSharp):
    1. writer = new StreamWriter($"/mnt.sdcard/VirtualRoomSwayData/Participant.csv");
    Code (CSharp):
    1. writer = new StreamWriter("/storage/extSdCard/VirtualRoomSwayData/Participant.csv");
    Code (CSharp):
    1. writer = new StreamWriter("/storage/emulated/0/VirtualRoomSwayData/Participant.csv");
    Any help greatly appreciated
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Don't use absolute paths (on any platform). Use the Application.persistentDataPath, like so:

    Code (CSharp):
    1.                 string fname = System.DateTime.Now.ToString("HH-mm-ss") + ".csv";
    2.                 string path = Path.Combine(Application.persistentDataPath, fname);
    3.                 file = new StreamWriter(path);
     
  3. Wez01

    Wez01

    Joined:
    Oct 28, 2017
    Posts:
    9
    Thanks JoeStrout. I just tried your code and also no file has been written. I tried with Permissions set to Internal and External SD Card, resetting the USB connection and restarting application from the headset (as I've read that can fix some things). Still nothing was written. Any other ideas?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No. I use that exact code all the time, and it works for me. Did you get an exception? Or could it be you just can't find the file (i.e. looking in the wrong place)?
     
  5. julienkay

    julienkay

    Joined:
    Nov 12, 2013
    Posts:
    171
    I've found that the Windows explorer sometimes displays stale data. (Deleted files were still shown, or created files were not shown even when they were there.) Replugging the headset usually fixes it, but that definitely caused me some unnecessary minutes of head-sratching before. Not sure whether that's the issue here though.
     
    mgear likes this.
  6. Wez01

    Wez01

    Joined:
    Oct 28, 2017
    Posts:
    9
    No exceptions or errors just no file. I've looked both at the "internal" path and in the external locations and there are definitely no files ever written.
    I want to write files to a folder on the Quest 2 when it is plugged in which is the SD Card from my understanding. I can't even create a new directory there.
    I've also checked Android>data>"apk build folder">files
     
    Last edited: Mar 22, 2021
  7. Wez01

    Wez01

    Joined:
    Oct 28, 2017
    Posts:
    9
    Yes I read that as well. I always replug before I check for the files in case that is the issue.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't think being plugged in has anything to do with it. The files will appear in /sdcard/Android/data/your-app-id.

    If they're not there, and you're not getting any exceptions, then I'm afraid I have no idea why it's not working for you.
     
  9. Wez01

    Wez01

    Joined:
    Oct 28, 2017
    Posts:
    9
    Thanks anyhow JoeStrout. I'm at a loss why it refuses to work.
    I've created a custom Android manifest file now but that hasn't solved anything.
     
  10. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    DirtyFred likes this.
  11. julienkay

    julienkay

    Joined:
    Nov 12, 2013
    Posts:
    171
    Maybe we misunderstood. The solution given before works when executed in an app on-device.
    Are you're trying to write files to the Quest from the Unity Editor or a Windows application?
     
    JoeStrout likes this.
  12. Wez01

    Wez01

    Joined:
    Oct 28, 2017
    Posts:
    9
    I think I'm going to have to work out how to look into the logs. I was hoping it was a simple fix but seems to not be.
     
  13. Wez01

    Wez01

    Joined:
    Oct 28, 2017
    Posts:
    9
    No it is just the apk application writing a directory and file is all I'm after
     
  14. DirtyFred

    DirtyFred

    Joined:
    May 17, 2014
    Posts:
    29
    Did anyone manage to fix this issue?

    I have tried both paths:
    /sdcard/Download/
    /storage/emulated/0/

    with internal, then external permissions set within player settings, plus an android manifest with read and write access to external and it still says that "access denied" when i try to read from the quest 2.

    Any suggestions?
     
  15. DirtyFred

    DirtyFred

    Joined:
    May 17, 2014
    Posts:
    29
    THAT WAS IT, the Android 9 fixed the issue for me.
    In case someone else has the same issue:
    Select Android Pie 9
    Internal Access Require
    Write Permission External (SDCard)

    AndroidManifest:

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:compileSdkVersion="29" android:compileSdkVersionCodename="10" android:installLocation="auto" package="REDACTED" platformBuildVersionCode="29" platformBuildVersionName="10">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true"/>
    <uses-feature android:glEsVersion="0x00030000"/>
    <supports-gl-texture android:name="GL_KHR_texture_compression_astc_ldr"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
    <uses-feature android:name="android.hardware.microphone" android:required="false"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
    <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false"/>
    <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false"/>
    <uses-feature android:name="android.hardware.vr.headtracking" android:required="true" android:version="1"/>
    <uses-feature android:name="oculus.software.handtracking" android:required="false"/>
    <uses-permission android:name="com.oculus.permission.HAND_TRACKING"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application android:allowBackup="false" android:extractNativeLibs="true" android:icon="@Mipmap/app_icon" android:label="@String/app_name" android:networkSecurityConfig="@XML/network_sec_config" android:requestLegacyExternalStorage="true">
    <activity android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenLayout|screenSize|uiMode" android:hardwareAccelerated="false" android:launchMode="singleTask" android:name="com.unity3d.player.UnityPlayerActivity" android:screenOrientation="landscape" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
    <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    <category android:name="com.oculus.intent.category.VR"/>
    </intent-filter>
    <meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
    <meta-data android:name="android.notch_support" android:value="true"/>
    <meta-data android:name="com.oculus.vr.focusaware" android:value="true"/>
    <meta-data android:name="android.notch_support" android:value="true"/>
    </activity>
    <meta-data android:name="unity.splash-mode" android:value="0"/>
    <meta-data android:name="unity.splash-enable" android:value="true"/>
    <meta-data android:name="notch.config" android:value="portrait|landscape"/>
    <meta-data android:name="unity.build-id" android:value="f9d86e1d-da2b-4f34-ac63-2b099cfad14d"/>
    <meta-data android:name="com.samsung.android.vr.application.mode" android:value="vr_only"/>
    <meta-data android:name="com.unity.xr.oculus.LowOverheadMode" android:value="false"/>
    <meta-data android:name="com.oculus.supportedDevices" android:value="quest|quest2"/>
    <meta-data android:name="com.oculus.handtracking.frequency" android:value="LOW"/>
    </application>
    </manifest>
     
  16. BlueSpirit

    BlueSpirit

    Joined:
    Jun 10, 2013
    Posts:
    33
    Awesome! Thanks! Worked for me as well.

    The only settings I needed were:
    Write Permission External (SDCard)

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    and

    android:requestLegacyExternalStorage="true" in the <application> tag
     
  17. luminator

    luminator

    Joined:
    Jun 24, 2016
    Posts:
    7
    Yeah I can also confirm this works. It's weird because from what I remember it used to work just by setting Write Permission External (SDCard) from Editor, but apparently now you have to also add the permission manually to manifest
     
    DirtyFred likes this.
  18. tlskillman

    tlskillman

    Joined:
    Dec 12, 2015
    Posts:
    16
    This worked for me. Thanks.
     
    JoeStrout likes this.
  19. amit-chai

    amit-chai

    Joined:
    Jul 2, 2012
    Posts:
    80
    Thanks! solved my issue as well!
     
    JoeStrout likes this.
  20. unity-slash

    unity-slash

    Joined:
    Mar 30, 2020
    Posts:
    4
    None of the proposals in this thread to access the folders inside the /sdcard directory worked for me, the only thing that worked for me is access to persistent data but only in that directory, I can't access Pictures or Movies directories.
    The only way I could do to grant read permissions to the /sdcard directory and subdirectories to my application is by manually granting the permission as follows:

    adb devices
    <once it recognized the device>
    adb shell
    <to be able to execute command inside my device>
    pm grant <com.company.proyectName> android.permission.READ_EXTERNAL_STORAGE
    <understanding that "com.company.projectName" is the name of the application / package>

    If you don't know the package name of your application you can do the following to get a list of all installed applications / packages:
    pm list packages

    Maybe it will help someone else.
     
  21. SapphireBrooks21

    SapphireBrooks21

    Joined:
    May 6, 2023
    Posts:
    1
    Hello! I've been at a dead end with trying to write files from the apk file of my game that I have installed on my Quest 2. The game targets Android 13, so permissions like writing external storage are more restricted compared to when this thread was last updated. I haven't had any success with Application.persistentDataPath paired with public folder directories like Documents that should be accessible. Write Permission External is enabled in Player Settings in Unity, and Storage Permissions are enabled for my app and SideQuest, which is what I used to install the apk. Any insights would be greatly appreciated.