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. Dismiss Notice

Question Save a file to downloads folder

Discussion in 'Android' started by jpom001, May 29, 2023.

  1. jpom001

    jpom001

    Joined:
    Dec 3, 2020
    Posts:
    69
    I created this code to save data from my persistent file path to a downloads folder, so that my testers can send me the log files back. Some testers say this dosent work for them. I have already added. I am getting this error


    Code (CSharp):
    1. 2023/05/29 13:08:19.847 Unity download logs fail execption: System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/Download/game-logs" is denied.
    2.  

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    What am I missing?
    Code (CSharp):
    1. public static void MoveFileToDownloadsFolder()
    2.     {
    3.         try {
    4.             string logfilename = "/logs.txt";
    5.             string sourcePath = Application.persistentDataPath + logfilename;
    6.  
    7.             // Get the path to the Downloads folder based on the platform
    8.             string downloadsPath = "";
    9. #if UNITY_ANDROID && !UNITY_EDITOR
    10.         downloadsPath = "/storage/emulated/0/Download/" +  "game-logs";
    11. #elif UNITY_IOS && !UNITY_EDITOR
    12.         downloadsPath = Path.Combine(Application.persistentDataPath, "..", "Documents",  "game-logs");
    13. #else
    14.             downloadsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", "game-logs");
    15. #endif
    16.  
    17.             if (File.Exists(sourcePath))
    18.             {
    19.                 File.Copy(sourcePath, downloadsPath, true);
    20.                 Debug.Log("File moved to Downloads folder: " + downloadsPath);
    21.             }
    22.             else
    23.             {
    24.                 Debug.LogError("File not found: " + sourcePath);
    25.             }
    26.         } catch (Exception e)
    27.         {
    28.             Debug.Log("download logs fail execption message: " + e.Message);
    29.             Debug.Log("download logs fail execption: " + e);
    30.         }
    31.         }
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Last edited: May 29, 2023
  3. jpom001

    jpom001

    Joined:
    Dec 3, 2020
    Posts:
    69
    Im android 12, this is my test device. I can manually go into my appinfo -> permissions and assign files and media. The download works then. At least when building to my device from unity.

    What are my options to simply deal with this. If I want a file that I can attach to an email.