- Home /
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!
Certainly Android (and iOS) absolutely delete "all the files from that app" when you delete an app from the device - this is inherent in both OS.
Answer by colchambers · Oct 30, 2012 at 08:44 PM
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
Answer by Bunny83 · Oct 29, 2012 at 02:41 PM
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 ;)
Answer by Clas · Jan 15, 2016 at 05:44 PM
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.
Hi @Clas - that looks very impressive. Just TBC, are you saying that is a way to save text data to android drive, EVEN IF the user deletes the application??
Can you explain please what your code does? thanks! Sorry if I missed the explanation.
i don't understand this code. how can i use this code? just ctrl+c -> ctrl+v on Start() method? and your package name mean com.companyname.gamename? right?
Answer by colchambers · Dec 11, 2012 at 06:17 PM
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.
It's how Android works, if you uninstall the data will be deleted as well.
Your answer
Follow this Question
Related Questions
Connect Andorid Phone and get error 1 Answer
Make sphere shoot to Touch.position error 1 Answer
errors building on android 0 Answers
Console error after selecting Android SDK location 1 Answer
Setting things up for android 1 Answer