Android writing to Application.persistentDataPath

I’ve been trying to do this for probably about 6 hours, and seriously regretting it haha.

string filePath =
    Application.persistentDataPath
    + @"/settings.xml";

the path

"/Android/data/com.mycompany.myapp/files/settings.xml"

USED to exist. I have the settings.xml file included in the Assets/Resources folder, so it should be copying. After not getting it to work, I uninstalled the app from my phone and reinstalled from scratch. That directory no longer gets created.

Any way I try, I either get an “access denied” or “part of the path does not exist” at the point of creating the FileStream with the path.

 private void SaveSettings() {
         Debug.Log (filePath);
         FileStream stream = new FileStream(
           filePath, FileMode.Create, FileAccess.Write);
         XmlWriter writer = XmlWriter.Create (stream);

This is seriously going to end up killing me haha!

I’ve tested almost the same today. It seems Unity doesn’t use

/Android/data/com.mycompany.myapp/files

for the persistant path.

The actual path that is used is

/data/data/com.mycompany.myapp/files

This path seems to work for me to save and load files, however i’m not able to find this path “manually” in one of my file browser apps.

It seems it’s located somewhere in the internal memory so it belongs to the security sandbox of your app. So your app can access this folder, but you can’t modify the files from outside your app.

Probably with root access you can access this directory, but i don’t have a “rooted” tablet available :wink:

I’ve found that the path android uses depends on the build settings. If you check development build or set player settings > android > configuration > write access = external sd card then the axternal mnt/android/data path is used. If internal only is selected and development build isn’t checked then data/data/ path is used.

that’s what I’ve found any way. I’ve also found that you don’t see changes in the sdcard while the phone is connected, or atleast when the app is running. Had to reconnect to see changes.

HTH

Colin

You can get your Android apps internal path directly from Unity without a plugin, even with permission WRITE_EXTERNAL_STORAGE. Similar code can be used to get any other path.

I spent 2 days on this :P. First I made my own Java plugin, but as I do not like to extend my current Activity or Application I ran into problems. Finally I pieced together how to do it directly from Unity with the following code:

string path = "";
#if UNITY_ANDROID && !UNITY_EDITOR
try {
         IntPtr obj_context = AndroidJNI.FindClass("android/content/ContextWrapper");
         IntPtr method_getFilesDir = AndroidJNIHelper.GetMethodID(obj_context, "getFilesDir", "()Ljava/io/File;");

         using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
            using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
               IntPtr file = AndroidJNI.CallObjectMethod(obj_Activity.GetRawObject(), method_getFilesDir, new jvalue[0]);
               IntPtr obj_file = AndroidJNI.FindClass("java/io/File");
               IntPtr method_getAbsolutePath = AndroidJNIHelper.GetMethodID(obj_file, "getAbsolutePath", "()Ljava/lang/String;");   
                                
               path = AndroidJNI.CallStringMethod(file, method_getAbsolutePath, new jvalue[0]);                    

               if(path != null) {
                  Debug.Log("Got internal path: " + path);
               }
               else {
                  Debug.Log("Using fallback path");
                  path = "/data/data/*** YOUR PACKAGE NAME ***/files";
               }
            }
         }
      }
      catch(Exception e) {
         Debug.Log(e.ToString());
      }
#else
      path = Application.persistentDataPath;
#endif

I hope it will help some devs without Java knowledge (like myself :)) to save some time and headache.

I don’t have a problem seeing the files on the internal storage. The problem is that on uninstall the data is gone. So it’s not persistent enough for me. /mnt/extSdCard instead. The camelcase is important.