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. Dismiss Notice

Android file system access in C++

Discussion in 'Android' started by ForrestSmithFRL, Jan 11, 2021.

  1. ForrestSmithFRL

    ForrestSmithFRL

    Joined:
    Aug 10, 2018
    Posts:
    4
    Hi. I have some C++ code that is compiled into
    libFoo.so
    . I am able to load this library on an Android device (Oculus Quest 2) and call functions via
    DllImport
    .

    What I would like to do is pass a
    string filePath
    path to my shared library. Then I want my C++ code to perform vanilla C++ file operations. For example:

    Code (cpp):
    1. std::ifstream inFile(filePath);
    2. inFile.read(....);
    3. inFile.close();
    I'm struggling to figure out what
    filePath 
    needs to be. I think it needs to be something to the effect of:

    Code (CSharp):
    1. string filePath = Path.Combine("jar:file://" + Application.dataPath + "!/assets/foo/bar/baz.json";
    I have verified that my content exists inside `/data/app/com.ham.eggs-gobbledegook==/base.apk/assets/foo/bar/baz.json`. The file is on my device. I just don't know how to open it in C++.

    Thanks!
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,624
    You won't be able to read files from inside apk that simply. Apk is a zip archive. When you use UnityWebRequest to access them, under the hood we perform reading from zip archive and decompression.
    If your files are directly on file system, such as Application.persistentDataPath, then C++ streams will work. Granted, files have to be on a directory you have permissions to access.
     
  3. ForrestSmithFRL

    ForrestSmithFRL

    Joined:
    Aug 10, 2018
    Posts:
    4
    Ahhh ok. I think I got it all now. Makes sense.

    I assume there's no way to configure Unity to install content to `persistentDataPath` when the `apk` is installed?
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,624
    no.
     
    ForrestSmithFRL likes this.
  5. natetrost

    natetrost

    Joined:
    Aug 21, 2020
    Posts:
    8
    There is a native AssetManager API included with the NDK for accessing files that are present in application bundles:
    https://developer.android.com/ndk/reference/group/asset

    This API works for APKs and also for app bundles and install-time Play Asset Delivery pack files.
     
    ForrestSmithFRL likes this.
  6. ForrestSmithFRL

    ForrestSmithFRL

    Joined:
    Aug 10, 2018
    Posts:
    4
    Using
    UnityWebInterface
    is there a way to get a list of files in a folder?

    For example assume I can use it to get the bytes for
    StreamingAssets/Foo/Bar/myFile.bin
    . Is there a way to get a list of all files in
    StreamingAssets/Foo/Bar
    so that I can create N requests for each file? On Android still.

    Thanks!
     
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,624
    No there is no direct way to get them. It is assumed that you know what you've put there.
     
    ForrestSmithFRL likes this.