Search Unity

Question Access to the Application.persistentDataPath is denied in Android

Discussion in 'Android' started by alex_periti, Dec 2, 2020.

  1. alex_periti

    alex_periti

    Joined:
    Oct 30, 2020
    Posts:
    1
    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

    1. Code (CSharp):
      1. private void Awake()
      2.      {
      3.          trackedImageManager = FindObjectOfType<ARTrackedImageManager>();
      4.          trackedImageManager.enabled = false;
      5.          updateLibraryButton.onClick.AddListener(() => StartCoroutine(DownloadNewImagesByUrl("https://upload.wikimedia.org/wikipedia/commons/9/97/The_Earth_seen_from_Apollo_17.jpg")));
      6.          LoadMutableReferenceLibrary();
      7.          if (trackedImageManager.descriptor.supportsMutableLibrary)
      8.          {
      9.              mutableLibrary = trackedImageManager.CreateRuntimeLibrary() as MutableRuntimeReferenceImageLibrary;
      10.              trackedImageManager.requestedMaxNumberOfMovingImages = 2;
      11.              trackedImageManager.trackedImagesChanged += ImageChanged;
      12.          }
      13.          else
      14.          {
      15.              Debug.Log("Device doesn't support MutableReferenceImageLibrary");
      16.          }
      17.      }
    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

    1. Code (CSharp):
      1. IEnumerator DownloadNewImagesByUrl(string url)
      2.      {
      3.          trackedImageManager.enabled = false;
      4.          UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(url);
      5.          yield return unityWebRequest.SendWebRequest();
      6.        
      7.          dynamicTexture = DownloadHandlerTexture.GetContent(unityWebRequest);
      8.        
      9.          SaveTextureAsPNG(dynamicTexture);
      10.      
      11.          LoadMutableReferenceLibrary();
      12.      }
    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

    1. Code (CSharp):
      1.   void LoadMutableReferenceLibrary()
      2.      {
      3.          trackedImageManager.enabled = false;
      4.          textures = Resources.LoadAll(Path.Combine(Application.persistentDataPath,folderPath), typeof(Texture2D));
      5.        
      6.          foreach (Texture2D texture in textures)
      7.          {
      8.              jobHandle = mutableLibrary.ScheduleAddImageJob(texture, texture.name, 0.2f);
      9.          }
      10.          jobLog.text = "Load Completed";
      11.          if (mutableLibrary != null)
      12.          {
      13.              Debug.Log("Image Library Count: " + mutableLibrary.count);
      14.              trackedImageManager.referenceLibrary = mutableLibrary;
      15.          }
      16.          trackedImageManager.enabled = true;
      17.      }
    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

    1. Code (CSharp):
      1. void SaveTextureAsPNG(Texture2D texture)
      2.      {
      3.          if (!Directory.Exists(Path.Combine(Application.persistentDataPath, folderPath)))
      4.          {
      5.              DirectoryInfo tmp = Directory.CreateDirectory(Path.Combine(Application.persistentDataPath, folderPath));
      6.              if (tmp != null)
      7.              {
      8.                  tmpText.text = tmp.FullName;
      9.              }
      10.              else
      11.              {
      12.                  tmpText.text = "No Folder";
      13.              }
      14.          }
      15.          try
      16.          {
      17.              File.WriteAllBytes(Path.Combine(Path.Combine(Application.persistentDataPath, folderPath), texture.name), texture.EncodeToPNG());
      18.          }
      19.          catch (System.Exception ex)
      20.          {
      21.              tmpText.text = ex.Message;
      22.          }
      23.          fileLog.text = texture.name;
      24.      }