Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by averscer · Sep 12, 2012 at 09:51 PM · androiderrorsavingio

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jan 25, 2016 at 10:16 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
9

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
8

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 ;)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jan 25, 2016 at 10:12 PM 1
Share

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.

avatar image jeonys1231 · Dec 12, 2017 at 10:45 AM 0
Share

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?

avatar image sandasumit · May 24, 2018 at 01:23 PM 0
Share

@clas - It worked like charm... saved lot of work....

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SeikoTheWiz · Jun 13, 2013 at 12:42 PM 0
Share

It's how Android works, if you uninstall the data will be deleted as well.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges