UnauthorizedAccessException: Access to the path is denied

I have anayltics in my Unity3D game and I realised that some Android devices throw the UnauthorizedAccessException when I am trying to create a folder on the device.

Have a look at the code that throws the exception:

static void SaveToCache(Entry entry, byte[] bytes) {
	if (entry.file != null) {
		string path = Path.GetDirectoryName(entry.file);
		try {
			if (!Directory.Exists(path)) Directory.CreateDirectory(path);
			File.WriteAllBytes(entry.file, bytes);			
		} catch (Exception ex) {
			Analytics.LogException(ex, "couldn't save to cache: " + path);

The code looks good for me, but perhaps I’m missing something… It does not throw on my test devices - I only have the stack trace from my analytics.

More relevant code is how I build the path:

static readonly char DirectorySeparator = Path.DirectorySeparatorChar;    

static string BundleFile(string id, string languageCode) {
	StringBuilder sb = new StringBuilder();
	return sb.ToString();

static string BundlePath() {
	StringBuilder sb = new StringBuilder();
	return sb.ToString();

Please tell me if someone out there has similar issues. Can’t I rely on Application.persistentDataPath ? Is there a better way to build the path for the cache files?

Edit: Found the problem. Application.persistentDataPath returns an empty string in rare cases/some devices. Anyone has this seen before?

Edit: for the solution see persitent data path handling in Unity with fallback · GitHub

I got around the problems with Application.persistantDataPath by writing a native Java plugin to get the internal and external storage paths explicitly. Here’s a page detailing how to do that: 日本入室强伦姧BD在线观看,日韩中文人妻无码不卡,卧室征服朋友人妻,人妻办公室被强奷

However, I still get a few UnauthorizedAccessException exceptions, though it’s not due to a blank path value, the path looks good now. I’m still investigating why I’m getting these errors.

All you need to do is grant the permissions, If you are not sure how, go check youtube//google