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 Android: Permission denied

Discussion in 'Scripting' started by AquaBall60_, Jun 11, 2023.

  1. AquaBall60_

    AquaBall60_

    Joined:
    May 13, 2023
    Posts:
    12
    My App ist working fine on Editor+PC. It uses an external Datafile.
    Even on android it works fine, as long as I embedd some sample datafile (fallback) as asset.
    But the Datafile might be edited by User, so I have to use it external, eg. on sdCard/Download (or similar).

    But I can't manage the Permissions. App always says at check "File exists", but "Access denied".
    It even doesn't ask me for granting Permission (neither on installation, nor at runtime).

    What am I doing wrong? (I am searching since 3 days)

    My Manifest.xml:
    Code (XML):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest
    3.    xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:tools="http://schemas.android.com/tools">
    5.     <application>
    6.         <activity android:name="com.unity3d.player.UnityPlayerActivity"
    7.                   android:theme="@style/UnityThemeSelector">
    8.             <intent-filter>
    9.                 <action android:name="android.intent.action.MAIN" />
    10.                 <category android:name="android.intent.category.LAUNCHER" />
    11.             </intent-filter>
    12.             <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    13.         </activity>
    14.     </application>
    15.     <!-- Benötigt, um Daten aus '/download' zu lesen -->
    16.     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    17.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    18.     <!-- WRITE only needed to write default sample for user-edit. -->
    19. </manifest>
    20.  
    My Program (reduced to essentials):
    Code (CSharp):
    1. ...
    2. using UnityEngine.Android;
    3.     void Start()
    4.     {
    5. #if PLATFORM_ANDROID
    6.         var permit = Permission.READ_EXTERNAL_STORAGE;
    7. // Non does work!
    8.         if (!Permission.HasUserAuthorizedPermission(permit))
    9.             Permission.RequestUserPermission(permit);
    10.  
    11.         // 'android.permission.READ_EXTERNAL_STORAGE: granted=true, flags=[ SYSTEM_FIXED|GRANTED_BY_DEFAULT ]'
    12.         // 'Permission' does not contain a definition for 'READ_EXTERNAL_STORAGE'
    13.         // 'Read_External_Storage'
    14.         // 'StorageRead'
    15.         // 'ReadExternalStorage'
    16.         // 'READ_EXTERNAL_STORAGE'
    17.         // 'android.permission.READ_EXTERNAL_STORAGE'
    18.  
    19.         //await Permissions.RequestAsync<Permissions.StorageRead>();
    20.         //await Permissions.RequestAsync<Permissions.StorageWrite>();
    21. #endif
    22.         TxDebugLog.text = $"Start:\n";
    23. // This needs permission "READ Download":
    24. // But fails on Android: "Access denied"
    25.         var data =getDynamicData(DatenFallBack);  // Fallback is als Asset embedded
    26.  
    27.         TxDebugLog.text += $"gelesen data {data}:\n";
    28. }
    (I can't even find a definition of 'Permission.cs', and VisualStudio doesn't offer any code completion.)
    Strings according to https://developer.android.com/reference/android/Manifest.permission don't work?!?
     
    Last edited: Jun 12, 2023
  2. AquaBall60_

    AquaBall60_

    Joined:
    May 13, 2023
    Posts:
    12
    Does nobody have a hint, what I am doing wrong?
    How to get a simply READ-permission on android?

    (I have read an tested hundreds of pages ...) :-(
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    I'm not sure permissions are your issue. Perhaps showing your code where you're trying to access the file is better. Plus, this is the scripting forum and not the Android forum, which would better serve you if you have Android related questions.

    So, if you want help with loading and saving a file, show that code. Otherwise, you might try the Android forum.

    To note, I've never had to do anything special to save and read from persistantdatapath on Android.
     
  4. AquaBall60_

    AquaBall60_

    Joined:
    May 13, 2023
    Posts:
    12
    Thanks for your answer.

    (I thought I had shown my code (see first post). )
    In the end, all reading takes place in the line 'getDynamicData()'. The code in there is very simple:
    It is verified that (folder and) file 'TanzDatei' exist, but StreamReader always fails: reporting {err.Message} "access denied". Therefore the 'Try Catch'. After being denyed, I read the Fallback (file embedded in Asset).
    Code (CSharp):
    1.         TxDebugLog.text += $"start streamReader '{Tanzdatei}':\n";
    2.         var fallback = false;
    3.         try
    4.         {
    5.             string zeile;
    6.               // ok in Win (and Editor), but fails ('denied') on Android:
    7.             using (StreamReader sr = new StreamReader(Tanzdatei))
    8.                 while ((zeile = sr.ReadLine()) != null)
    9.                      ZeileSammeln(zeile);
    10.         }
    11.         catch (System.Exception err)
    12.         {
    13.             Debug.Log($"streamReader '{Tanzdatei}' misslungen:");
    14.             Fatal($"streamReader '{Tanzdatei}' misslungen\n\t\t====> FallBack lesen!\n{err.Message}");
    15.             fallback=true;
    16.         }        
    17.         if (fallback)
    18.               foreach (var zeile in DatenFallBack.text.Split("\n"))
    19.                      ZeileSammeln(zeile);        // works
    20.  
    Yes, it is a pure Unity Android problem.
    On Windows, reading from both, file and asset, works fine. There are no permission issues.

    My problem is a Unity problem. That's why I posted it here. Another forum might not be able to tell me how in Unity the permissions and manifest are managed.
    The main problem is that I am never asked for a permission neither when installing nor running.
    So it's about: How I can install a program with permissions as Unity does that 'fully automatically'.
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,083
    Last edited: Jun 19, 2023
    CodeRonnie likes this.
  6. AquaBall60_

    AquaBall60_

    Joined:
    May 13, 2023
    Posts:
    12
    Thx, a lot!!!
    This might be the answer!
    Sounds legit.

    I'l give it a try on weekend.
    thk you very much