Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Share single file between many android apps (Unity)

Discussion in 'Scripting' started by EpicVR-, Sep 16, 2022.

  1. EpicVR-

    EpicVR-

    Joined:
    Dec 10, 2018
    Posts:
    5
    Hi,
    I have two apps (this same project) with two different package names:

    Code (CSharp):
    1.  
    2. com.app.test1
    3. com.app.test2
    4.  
    And I have two methods:

    Code (CSharp):
    1.  
    2.     public void AddToFile(string value)
    3.     {
    4.         var path = Path.Combine(URL, fileName);
    5.  
    6.         using (var fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
    7.         {
    8.             using (var sw = new StreamWriter(fs))
    9.             {
    10.                 sw.WriteLine(value);
    11.  
    12.                 Debug.Log("AddToFile!");
    13.             }
    14.         }
    15.  
    16.         LoadFromFile();
    17.     }
    18.  
    19.  
    20.     public void LoadFromFile()
    21.     {
    22.         var path = Path.Combine(URL, fileName);
    23.  
    24.         if (!File.Exists(path))
    25.         {
    26.             Debug.Log("File not exists!");
    27.             return;
    28.         }
    29.  
    30.         using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    31.         {
    32.             using (var sr = new StreamReader(fs))
    33.             {
    34.                 var text = sr.ReadToEnd();
    35.                 fileValue.text = text;
    36.  
    37.                 Debug.Log("LoadFromFile!");
    38.             }
    39.         }
    40.     }
    41.  
    And everything works fine for one app. I can load file I can add some text to existing file (or create new one), but when I try to open this same file from second app I have an error:

    Code (CSharp):
    1. System.UnauthorizedAccessException - "Access to the path PATH is denied"
    Unity Version: 2021.3.4f1
    Target platform: Android 11

    I also add permissions to the android manifest (read/write).
    Any sugestions?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    to what path you are saving?

    if its some public path, like downloads/ or so, i think it should work.
     
  3. EpicVR-

    EpicVR-

    Joined:
    Dec 10, 2018
    Posts:
    5

    Yes, It is a public folder:

    Code (CSharp):
    1.     private string URL => Path.Combine(GetAndroidInternalFilesDir(), "Download");
    2.  
    3.  
    4. //I found this solution in web:
    5.     private static string GetAndroidInternalFilesDir()
    6.     {
    7.         string[] potentialDirectories = new string[]
    8.         {
    9.         "/mnt/sdcard",
    10.         "/sdcard",
    11.         "/storage/sdcard0",
    12.         "/storage/sdcard1"
    13.         };
    14.  
    15.         if (Application.platform == RuntimePlatform.Android)
    16.         {
    17.             for (int i = 0; i < potentialDirectories.Length; i++)
    18.             {
    19.                 if (Directory.Exists(potentialDirectories[i]))
    20.                 {
    21.                     return potentialDirectories[i];
    22.                 }
    23.             }
    24.         }
    25.  
    26.         Debug.LogError("Path not exists!");
    27.         return "";
    28.     }
    As I said: If I use only one app - all works fine. But when I try to open this file via second app - I just see an error.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    ah ok, not sure then, could try the force internal or force external player settings too, if any difference.
    or maybe its more strict on the recent android versions?
     
  5. EpicVR-

    EpicVR-

    Joined:
    Dec 10, 2018
    Posts:
    5
    Yes, I have few more lines in code:
    Code (CSharp):
    1.         if (!hasExternalStorageRead)
    2.             Permission.RequestUserPermission(Permission.ExternalStorageRead);
    3.  
    4.         if (!hasExternalStorageWrite)
    5.             Permission.RequestUserPermission(Permission.ExternalStorageWrite);
    6.  
    7.         if (!hasManageExternalStorage)
    8.             Permission.RequestUserPermission("android.permission.MANAGE_EXTERNAL_STORAGE");

    In Android 11, they made life and access to such files difficult. As if files are protected for only one application.
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
  7. EpicVR-

    EpicVR-

    Joined:
    Dec 10, 2018
    Posts:
    5

    This works fine, but works with copies of the file. Which also boils down to the fact that I don't have permission to delete the file and force me to create multiple copies. Additionally: However, it forces you to manually enter the path and select the file.
     
  8. EpicVR-

    EpicVR-

    Joined:
    Dec 10, 2018
    Posts:
    5
    Probably due to the current security in Android 11 - this option is not available. If something changes, I will let you know or be ahead of my update :)