- Home /
Application.persistentDataPath returning null on certain Android devices
Recently using some exception handling / crash tracking software I noticed a percentage of users on android devices were throwing a UnauthorizedAccessException when trying to save!
Doing some research I saw a few developers complaining about issues regarding changing paths being returned depending on internal vs external storage so I began logging out what the Application.persistentDataPath was returning on those users throwing the exception.
To my surprise it seems that when I logged out Application.persistentDataPath it was not different paths than expected but actually returning NULL. My save code uses is pretty simple and I create the file path like so:
string filePath = System.IO.Path.Combine( Application.persistentDataPath, "DI.txt" );
Any idea on how / why / when the Application.persistentDataPath should or would return NULL?
This does not happen to our ios users!!!!
I'm not an Android expert, so I'm not sure entirely how to go about checking this theory out, but can you check the permissions that are set for the Android build? I figured Unity just sets permissions based on what calls are made, but it's worth checking to see if you have the ability to write files both internally and externally. $$anonymous$$ight be a bug if it isn't set?
A quick Google search seems to suggest that returning NULL for a public data path is a permissions thing, and there seems to be a few articles out there on how to edit the manifests to add/remove permissions for Android builds.
Sounds like the best approach is to avoid Application.persistantDataPath, and write a Java plugin to get the Internal storage directory directly from the OS. Seems that the persistantDataPath can change based on if you have an SD card inserted or a USB cable plugged in anyway, not to mention why it returns null or String.Empty for some users in particular situations.
Answer by paulojorgemaiapereira · Aug 11, 2015 at 03:43 PM
On my case Application.persistentDataPath was returning a empty string. The problem was on the device. I restart the device and the Application.persistentDataPath start working fine again.
Answer by sandolkakos · Aug 21, 2018 at 01:43 PM
My problem was solved when I noticed the carriage return (/r) character in the ProductName of my project. Look here: https://forum.unity.com/threads/app...-string-on-some-machines.384000/#post-3604512