Android Question Random access with Android 13

Jeanc161

Member
Licensed User
Longtime User
Hi guys!
It's been a while but i'm king of stuck in a snag with file access on android 13, don't have nay problem with earlier version of android but when i read the specs for android 13 accessing File.DirDefaultExternal or File.DirInternal (In some cases) it always return an error as the file can't be located either by the path for DirInternal or DidDefaultExternal and does not want to read or write to that specific folder.
So My question is Has anyone been able to circonvent this problem with android 13 and able to save and read file on those directories
wich would be Storage/Emulated/0/app.domain.com/android/data/files for external default or the internal dir for the same thing

The funny thing is i have others files that are read and save on DirInternal on the same device that works correctly that where created under android 7 but with the new files created with B4A version 11 it does not seems to work using the same method using the random access library to store list of array object.

I included some sample code and the manifest stuff for permissions if interested

I modify the code so i use user type instead of class in the array. The array is now only used to store temporary data from the form that can be modified in the medList of objects that now is of user type like i use for all other type of random.writeB4XObject. but so far still no luck for writing or reading those files if android/data/files
on android device with version 13 on my old cell Motorola Stylus using android 7 where there is no problem

The manifest file
Manifest Editor:
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)

addPermission(android.permission.READ_MEDIA_AUDIO)  
AddPermission(android.permission.SYSTEM_ALERT_WINDOW)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.ACCESS_WIFI_STATE)

This is some extract of code that i use inside the activity and i don't use the B4XPage
Activity Code:
Global Vars
    Public random As RandomAccessFile
    Public rp As PermissionsManager
    Public medList As List
    Public medData(9) As medCounter
    Type medsParm( medId As String,medDescription As String,medMaxQty As Int,medRemoveQty As Int,medCount As Int,medHours As Int,affectClock As Int)


Sub Activity_Resume
medList.initialize
      ' permission not granted request it to write to external storage
      rp.CheckAndRequestPermission(PermissionsManager.DANGEROUS_WRITE_EXTERNAL_STORAGE)
      wait For Activity_PermissionResult (Permission As String,resultat As Boolean)
          If resultat Then
            isExternalStorage = True
            ToastMessageShow("Permission WRITEExternal storage autorised",True)
        Else
            isExternalStorage = False
            ToastMessageShow("External WRITE storage access denied",True)
        End If  
End Sub

'Activity_Pause
Sub Activity_Pause (UserClosed As Boolean)
    'this works
    Log("activity paused no action taken")
    File.WriteString(File.DirInternal,countDownFile,countDownTime)
    File.WriteString(File.DirInternal,"PAUSED.PP","1")
' + Other stuff for socket closing
End sub

' this is how i set my values into the class object
' using the form on screen with EditBox, labels and CheckBox
Sub getMedDataValues
    ' set the values fromarray object to edit views
    If labl0.IsInitialized = True Then
    For i = 0 To medList.Size-1
        Dim medp As medsParm
            medp.Initialize
            medp = medList.Get(i)
            medData(i).medId = medp.medId
            medData(i).medMaxQty = medp.medMaxQty
            medData(i).medRemoveQty = medp.medRemoveQty
            medData(i).medCount = medp.medCount
            medData(i).medDescription = medp.medDescription
            medData(i).medHours = medp.medHours
            medData(i).affectClock = medp.affectClock

    Next

        Edit00.Text = medData(0).medId : Edit10.text = medData(0).medmaxQty: Edit20.TEXT = medData(0).medRemoveQty: Edit30.text = medData(0).medCount : Edit40.text = medData(0).medHours
        Edit01.Text = medData(1).medId : Edit11.text = medData(1).medmaxQty: Edit21.TEXT = medData(1).medRemoveQty: Edit31.text = medData(1).medCount : Edit41.text = medData(1).medHours
        Edit02.Text = medData(2).medId : Edit12.text = medData(2).medmaxQty: Edit22.TEXT = medData(2).medRemoveQty: Edit32.text = medData(2).medCount : Edit42.text = medData(2).medHours
        Edit03.Text = medData(3).medId : Edit13.text = medData(3).medmaxQty: Edit23.TEXT = medData(3).medRemoveQty: Edit33.text = medData(3).medCount : Edit43.text = medData(3).medHours
        Edit04.Text = medData(4).medId : Edit14.text = medData(4).medmaxQty: Edit24.TEXT = medData(4).medRemoveQty: Edit34.text = medData(4).medCount : Edit44.text = medData(4).medHours
        Edit05.Text = medData(5).medId : Edit15.text = medData(5).medmaxQty: Edit25.TEXT = medData(5).medRemoveQty: Edit35.text = medData(5).medCount : Edit45.text = medData(5).medHours
        Edit06.Text = medData(6).medId : Edit16.text = medData(6).medmaxQty: Edit26.TEXT = medData(6).medRemoveQty: Edit36.text = medData(6).medCount : Edit46.text = medData(6).medHours
        Edit07.Text = medData(7).medId : Edit17.text = medData(7).medmaxQty: Edit27.TEXT = medData(7).medRemoveQty: Edit37.text = medData(7).medCount : Edit47.text = medData(7).medHours
        CheckBox00.checked = valBoolean(medData(0).affectClock)    : CheckBox01.checked = valBoolean(medData(1).affectClock) : CheckBox02.checked = valBoolean(medData(2).affectClock) : CheckBox03.checked = valBoolean(medData(3).affectClock)
        CheckBox04.checked = valBoolean(medData(4).affectClock) : CheckBox05.checked = valBoolean(medData(5).affectClock) : CheckBox06.checked = valBoolean(medData(6).affectClock) : CheckBox07.checked = valBoolean(medData(7).affectClock)
        labl0.Text = medData(0).medDescription : Labl1.text = medData(1).medDescription
        Labl2.Text = medData(2).medDescription : Labl3.text = medData(3).medDescription
        Labl4.Text = medData(4).medDescription : Labl5.text = medData(5).medDescription
        Labl6.Text = medData(6).medDescription : Labl7.text = medData(7).medDescription
        ' update the medList with actual data
        ' put back the values in medList
        For i = 0 To medList.Size-1
            Dim medp As medsParm
                medp.Initialize
                medp.medId = medData(i).medId
                medp.medMaxQty = medData(i).MaxQty
                medp.medRemoveQty = medData(i).medRemoveQty
                medp.medCount = medData(i).medCount
                medp.medDescription = medData(i).medDescription
                medp.medHours = medData(i).medHours
                medp.affectClock = medData(i).affectClock
                medList.Set(i,medp)        
        Next

    End If
End Sub

Sub setMedDataValues
    ' get the values from the medata and the editView on layout medData

    If labl0.IsInitialized = True Then
        medData(0).medId = Edit00.Text : medData(0).medMaxQty = Edit10.text : medData(0).medRemoveQty = Edit20.text : medData(0).medCount = Edit30.text : medData(0).medHours = Edit40.text
        medData(1).medId = Edit01.Text : medData(1).medMaxQty = Edit11.text : medData(1).medRemoveQty = Edit21.text : medData(1).medCount = Edit31.text : medData(1).medHours = Edit41.text
        medData(2).medId = Edit02.Text : medData(2).medMaxQty = Edit12.text : medData(2).medRemoveQty = Edit22.text : medData(2).medCount = Edit32.text : medData(2).medHours = Edit42.text
        medData(3).medId = Edit03.Text : medData(3).medMaxQty = Edit13.text : medData(3).medRemoveQty = Edit23.text : medData(3).medCount = Edit33.text : medData(3).medHours = Edit43.text
        medData(4).medId = Edit04.Text : medData(4).medMaxQty = Edit14.text : medData(4).medRemoveQty = Edit24.text : medData(4).medCount = Edit34.text : medData(4).medHours = Edit44.text
        medData(5).medId = Edit05.Text : medData(5).medMaxQty = Edit15.text : medData(5).medRemoveQty = Edit25.text : medData(5).medCount = Edit35.text : medData(5).medHours = Edit45.text
        medData(6).medId = Edit06.Text : medData(6).medMaxQty = Edit16.text : medData(6).medRemoveQty = Edit26.text : medData(6).medCount = Edit36.text : medData(6).medHours = Edit46.text
        medData(7).medId = Edit07.Text : medData(7).medMaxQty = Edit17.text : medData(7).medRemoveQty = Edit27.text : medData(7).medCount = Edit37.text : medData(7).medHours = Edit47.text
        medData(0).affectClock = booleanVal(CheckBox00.checked) : medData(1).affectClock = booleanVal(CheckBox01.Checked)
        medData(2).affectClock = booleanVal(CheckBox02.checked) : medData(2).affectClock = booleanVal(CheckBox03.Checked)
        medData(4).affectClock = booleanVal(CheckBox04.checked) : medData(5).affectClock = booleanVal(CheckBox05.Checked)
        medData(6).affectClock = booleanVal(CheckBox06.checked) : medData(7).affectClock = booleanVal(CheckBox07.Checked)
        medData(0).medDescription = labl0.Text : medData(1).medDescription = Labl1.text
        medData(2).medDescription = Labl2.Text : medData(3).medDescription = Labl3.text
        medData(4).medDescription = Labl4.Text : medData(5).medDescription = Labl5.text
        medData(6).medDescription = Labl6.Text : medData(7).medDescription = Labl7.text

        ' update the medList with actual data
        For i = 0 To medList.Size-1
            Dim medp As medsParm
                medp = medList.Get(i)
                medp.Initialize
                medp.medId = medData(i).medId
                medp.medMaxQty = medData(i).MaxQty
                medp.medRemoveQty = medData(i).medRemoveQty
                medp.medCount = medData(i).medCount
                medp.medDescription = medData(i).medDescription
                medp.medHours = medData(i).medHours
                medp.affectClock = medData(i).affectClock
                medList.Set(i,medp)
        Next
End if
  
End Sub

Sub booleanVal(value As Boolean) As Int
    If value = True Then
        Return 1
    Else
        Return 0  
    End If
End Sub
Sub valBoolean(Value As Int) As Boolean
    If Value = 1 Then
        Return True
    Else
        Return False  
    End If
End Sub

Sub setMedDataArray
    ' set the medData with initialvalues of 0
    ' trying to get a folder values that works with
    ' permission manager but i already have the permissions
    'Dim folder As String = rp.GetSafeDirDefaultExternal("")
    folder = "/storage/emulated/0/Documents/"
    'Dim folders As List
    '    folders.Initialize
'        folders = rp.GetAllSafeDirsExternal("medData")
'        folder = folders.Get(0)
    ' initialise the values by default on the class object
    If medData(0).IsInitialized = False Then
      For i = 0 To 8
        medData(i).initialize
        medData(i).setid(i+1)
        medData(i).setmaxqty(0)
        medData(i).setRemoveQty(1)
        medData(i).setMedCount(100,True)
        medData(i).medDescription = "Medocs x"
        medData(i).affectClock = 1
        medList.Add(medData(i))
      Next
    End If
    ' this part work for some reason
    Dim lbls As checkboxLabel
        random.Initialize(File.DirInternal,labelsFileName,False)
        lbls = random.ReadB4XObject(random.CurrentPosition)
        random.Close

    setMedDAtaLabels(lbls)

    If File.Exists(file.DirInternal,"medData.txt") = True Then
       random.Initialize(File.DirInternal,"medData.txt",True)        ' this block
       random.WriteB4XObject(medList,0)    ' write from beginning of file
       random.close
    End If
End Sub

Sub readMedDataArray
    ' get or set the values of medData for labels and medType
    ' defined in the module
    'Dim folder As String = rp.GetSafeDirDefaultExternal("")
    folder = File.DirInternal
    'Dim folders As List
    '    folders.Initialize
    '    folders = rp.GetAllSafeDirsExternal("medData")
     '   folder = folders.Get(0)

    Log("Default Folder=" & folder)
    If medData(0).IsInitialized = False Then
        'File.Delete(folder,"medData.txt")
        If File.Exists(folder,"medData.txt") = False Then
            ' create default values in  medArray()
            setMedDataArray
            random.Initialize(folder,"medData.txt",True)
            random.WriteB4XObject(medList,0)
            random.close
        Else
      
           random.Initialize(folder,"medData.txt",False)
           medList = random.ReadB4XObject(0)
           random.close  
         
        End If
    Else if medData(0).IsInitialized = True Then
        random.Initialize(folder,"medData.txt",False)
        medList = random.ReadB4XObject(0)
        random.close
    End If
  
End Sub

Sub saveMedDataArrayToFile
    ' save the medData() to file
    'Dim folder As String = rp.GetSafeDirDefaultExternal("")
    folder = "/storage/emulated/0/Documents/"

    'Dim folders As List
     '   folders.Initialize
'        folders = rp.GetAllSafeDirsExternal("medData")
'        folder = folders.Get(0)
        Log("Safe folder is " & folder)
    File.Delete(folder,"medData.txt")
    random.Initialize(folder,"medData.txt",True)
    random.WriteB4XObject(medList,0)
    random.close
End Sub

So anyone who have the same problem and was able to make it work on android 13 or 14 please let me know
i tried many things not listed here and still the same problem with all of them and the funny thing is when i request permissions using Permission Manager or the other one RuntimePermission i get the same result
So i wonder where can we save the application data if all the Folders are blocked by the stupid android developpers, do we have to put our app on PlayStore for it to work with a private app ???????????

Thanks guys
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
'this works Log("activity paused no action taken") File.WriteString(File.DirInternal,countDownFile,countDownTime) File.WriteString(File.DirInternal,"PAUSED.PP","1")
These lines work because there are no restrictions on File.DirInternal.

The problem is elsewhere in the project (I didn't have the patience to read everything, also because I "don't love" multi-statement lines).
 
Upvote 0

Jeanc161

Member
Licensed User
Longtime User
These lines work because there are no restrictions on File.DirInternal.

(I didn't have the patience to read everything, also because I "don't love" multi-statement lines).

it works everywhere except in android 13 and 14 and as for multi-line statements too bad but it is a simple way to keep the code small on screen when displayed and it keep it compact on the ide cause when you have 3000 lines of code the ide became less performant even on a fast computer so sorry if you did not like that but that the way i like it.
 
Upvote 0
Top