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
0
Question by Calvin2274 · Sep 10, 2014 at 10:38 AM · androidsave datapersistentdatapath

internal persistentDataPath with WRITE_EXTERNAL_STORAGE permission

Hi,

I having problem about using persistentDataPath on Android.

I know that persistentDataPath will return different data path base on "write access" setting. same of permission WRITEEXTERNAL__STORAGE in AndroidManifest.xml

Internal Only : data/data/xxxx/files/ ( Player cannot get file inside this path without root )

External ( SD Card ) : Android/data/com.xxx.xx/files/ ( Player can get file inside this path without root )

However, I am going to save game data into Internal persistentDataPath, but i have WRITE_ EXTERNAL_STORAGE permission because i want to use expansion file( .obb ), but now my game data saved at SD Card and player can get file and edit.

So my question is, how can i save data into internal persistentDataPath with WRITEEXTERNAL__STORAGE permission ?

=== UPDATE ===

I am trying to hard code internal persistentDataPath and move file from public persistentDataPath, it was success, i can move it and also read and delete __AT THE FIRST TIME_, but when i quit app and launch again, i got a __UnauthorizedAccessException_ and blocked at Delete File action.

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Calvin2274 · Sep 11, 2014 at 10:14 AM

Finally, i found that UnauthorizedAccessException caused by System.IO.File.Move() ,

I took many test, to read, to write, to delete, i found that i can read and write data directly before called Move(),

so I used ReadAllBytes, WriteAllBytes and then Delete() the old file instead

problem solved.

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 18, 2016 at 08:55 AM

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 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 GiyomuGames · May 06, 2017 at 10:57 PM 0
Share

Thanks I'm going to use this! I have an issue where the persistentDataPath is pointing to the SD card of players, but my game doesn't have the permission to write there. Your code will help me for when this happens!

avatar image
0

Answer by Yury-Habets · Sep 10, 2014 at 12:41 PM

Can you save your data in PlayerPrefs http://docs.unity3d.com/ScriptReference/PlayerPrefs.html ? It cannot be modified by the user without root access. For storing several string or number values that would be pretty convenient.

Otherwise, I can't remember a simple way of having WRITE_EXTERNAL_STORAGE and having your data path in the internal storage.

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 Calvin2274 · Sep 11, 2014 at 02:24 AM 0
Share

no, cause my game datas is a sqlite file....

do you think can i hard code the internal data path to do this ?

avatar image
0

Answer by infosekr · Sep 10, 2015 at 06:35 PM

You need to write a native android plugin to return the internal storage path, and not use the Application.persistantDataPath. Look at the code in this example: http://www.supersegfault.com/writing-android-plugins-for-unity/

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

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

6 People are following this question.

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

Related Questions

Can other android apps delete save data of Application.persistentDataPath? 1 Answer

Android: Trying to reset save data on device when pushing a new build! 1 Answer

Save an image to PersistentDataPath then access it again 1 Answer

How to clean Application.persistentDataPath on Android 1 Answer

PlayerPrefs file wrong stored location 0 Answers


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