Access to the Application.persistentDataPath is denied in Android
I'm trying to set up a MutableReferenceLibrary at runtime, since the library isn't persistent i need to download file by URL and save it in a folder, and then i have to add it to the Library. Every time i start the app, I want to load file in the folder and add them to the Library. I'm not able to debug this on my phone/emulator since VS doesn't detect them so i can't know what's wrong, i only know that Download function is executed, but after that, the app seems to stuck at SaveTextureAsPNG(), because the function print "Access to the path /storage/emulated/0/Android/data/companyname/files/Assets/Resources/Textures is denied". I really can't figure out what's wrong
private void Awake()
{
trackedImageManager = FindObjectOfType<ARTrackedImageManager>();
trackedImageManager.enabled = false;
updateLibraryButton.onClick.AddListener(() => StartCoroutine(DownloadNewImagesByUrl("https://upload.wikimedia.org/wikipedia/commons/9/97/The_Earth_seen_from_Apollo_17.jpg")));
LoadMutableReferenceLibrary();
if (trackedImageManager.descriptor.supportsMutableLibrary)
{
mutableLibrary = trackedImageManager.CreateRuntimeLibrary() as MutableRuntimeReferenceImageLibrary;
trackedImageManager.requestedMaxNumberOfMovingImages = 2;
trackedImageManager.trackedImagesChanged += ImageChanged;
}
else
{
Debug.Log("Device doesn't support MutableReferenceImageLibrary");
}
}
Function DownloadImageByUrl basically uses UnityWebRequest to retrieve a image of Wikipedia, and Call SaveTextureAsPNG, after it's saved it should call LoadMutableReferenceLibrary to update the MutableReferenceImageLibrary
IEnumerator DownloadNewImagesByUrl(string url)
{
trackedImageManager.enabled = false;
UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(url);
yield return unityWebRequest.SendWebRequest();
dynamicTexture = DownloadHandlerTexture.GetContent(unityWebRequest);
SaveTextureAsPNG(dynamicTexture);
LoadMutableReferenceLibrary();
}
Function LoadMutableReferenceLibrary it's used to load all the Textures2D that is sacved in the folder Textures and add them to ReferenceImageLibrary, the jobLog.text is used for debugging purpose
void LoadMutableReferenceLibrary()
{
trackedImageManager.enabled = false;
textures = Resources.LoadAll(Path.Combine(Application.persistentDataPath,folderPath), typeof(Texture2D));
foreach (Texture2D texture in textures)
{
jobHandle = mutableLibrary.ScheduleAddImageJob(texture, texture.name, 0.2f);
}
jobLog.text = "Load Completed";
if (mutableLibrary != null)
{
Debug.Log("Image Library Count: " + mutableLibrary.count);
trackedImageManager.referenceLibrary = mutableLibrary;
}
trackedImageManager.enabled = true;
}
Function SaveImageAsPNG() simply should save the Texture2D that i have previously downloaded in the Textures folder, the tmpText print the Access Denied error mentioned before
void SaveTextureAsPNG(Texture2D texture)
{
if (!Directory.Exists(Path.Combine(Application.persistentDataPath, folderPath)))
{
DirectoryInfo tmp = Directory.CreateDirectory(Path.Combine(Application.persistentDataPath, folderPath));
if (tmp != null)
{
tmpText.text = tmp.FullName;
}
else
{
tmpText.text = "No Folder";
}
}
try
{
File.WriteAllBytes(Path.Combine(Path.Combine(Application.persistentDataPath, folderPath), texture.name), texture.EncodeToPNG());
}
catch (System.Exception ex)
{
tmpText.text = ex.Message;
}
fileLog.text = texture.name;
}
Answer by kordou · Jan 20, 2021 at 02:02 PM
I order to find the error you can use the asset Log Viewer. you can read the log from your mobile. If you use System.IO The access denied may be caused by the path.
you have to declare the path like following
string url = "https://aserver.com/file.zip"; string path = System.IO.Path.Combine(Application.persistentDataPath, System.IO.Path.GetFileName(url));
and then use this path to download the file