Android Question WRITE_EXTERNAL_STORAGE permission

Sergey_New

Well-Known Member
Licensed User
Longtime User
How can I set this permission if targetSdkVersion 33, and then create a “MyApplication” folder?
With targetSdkVersion 29 it was easy.
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
I only need to create a folder with the application name given. I don’t understand where you can insert this name in example Externalstorage ?
 
Upvote 0

AlfaizDev

Well-Known Member
Licensed User
I only need to create a folder with the application name given. I don’t understand where you can insert this name in example Externalstorage ?​
When you use the Externalstorage Library and summon the SelectDIR event, it will open a dialog box that allows the user to determine the virtual folder that the application can always access without asking the user to select the folder again, unless the folder or application is deleted.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
In the Externalstorage example I added a button to read the created folder named "123".
B4X:
Private Sub Button1_Click
  Dim lstFiles As List = File.ListFiles(File.DirRootExternal & "/123")
  ToastMessageShow("Number of files: " & lstFiles.Size, True)
End Sub
Regardless of whether there are files in this folder, lstFiles.Size=0.
What did I do wrong?
 

Attachments

  • ExternalStorage.zip
    4.9 KB · Views: 90
Upvote 0

AlfaizDev

Well-Known Member
Licensed User
In the Externalstorage example I added a button to read the created folder named "123".
B4X:
Private Sub Button1_Click
  Dim lstFiles As List = File.ListFiles(File.DirRootExternal & "/123")
  ToastMessageShow("Number of files: " & lstFiles.Size, True)
End Sub
Regardless of whether there are files in this folder, lstFiles.Size=0.
What did I do wrong?
B4X:
Private Sub Button1_Click
'    Dim lstFiles As List = File.ListFiles(File.DirRootExternal & "/123")
'    ToastMessageShow("Number of files: " & lstFiles.Size, True)
    
    For Each f As ExternalFile In Storage.ListFiles(Storage.Root)
        ToastMessageShow("Number of files: " & f.Length, True)
    Next
End Sub

I think the code should be like this
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
I think the code should be like this
Your code is not working correctly.
If there are no files in the folder "123", the code does not return anything; if there is only one file in the folder, then the number 12759 is returned.
In message #10 I attached an example for verification.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
I checked the file, 12759 is the number of characters in the file.
 
Upvote 0

AlfaizDev

Well-Known Member
Licensed User
I checked the file, 12759 is the number of characters in the file.
It is not true that it comes in file size, but in bytes
Try now with converting between units
B4X:
Private Sub Button1_Click
'    Dim lstFiles As List = File.ListFiles(File.DirRootExternal & "/123")
'    ToastMessageShow("Number of files: " & lstFiles.Size, True)
   
    For Each f As ExternalFile In Storage.ListFiles(Storage.Root)
    '    ToastMessageShow("Number of files: " & f.Length, True)
        Log("Number of files: " & NumberSuffix(f.Length))
    Next
End Sub

Sub NumberSuffix(N As Double) As String

    If N < 0 Then
        Return "-" & NumberSuffix(-N)
    End If
 
    Dim Suffix() As String = Array As String("", "k", "M", "B", "T")
    '''Dim Suffix() As String = Array As String("", " Thousand", " Million", " Billion", " Trillion")

    Dim Thousands As Int = 0
    Do While N >= 1000 And Thousands < Suffix.Length - 1
        Thousands = Thousands + 1
        N = N / 1000
    Loop

    If Thousands = 0 Then
        '2 decimal places per example of monetary amount including cents
        Return NumberFormat2(N, 1, 2, 2, False)
    End If
 
    'sufficient decimal places for precision required
    Dim MaxDecimalPlaces As Int = 0
    Do While MaxDecimalPlaces < 5    'absolute maximum 5 decimal places
        Dim RelativeError As Double = Abs(N - Round2(N, MaxDecimalPlaces)) / N
        If RelativeError < 0.007 Then    'eg 0.7% (or whatever precision you need)
            Exit
        End If
        MaxDecimalPlaces = MaxDecimalPlaces + 1
    Loop
 
    Return NumberFormat2(N, 1, 0, MaxDecimalPlaces, False) & Suffix(Thousands)

End Sub


log:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
Can use persistant uri!
size of files: 3.00
size of files: 2.55M
 
Upvote 0

AlfaizDev

Well-Known Member
Licensed User
I checked the file, 12759 is the number of characters in the file.
Because for text files, each character is approximately equal to one byte
So it's normal
The number of letters must be equal to the number of bytes
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
The user have to select the right dir first.
Thank you!
I did everything as required in Erel example.
I'm completely confused, could you please correct my example to work correctly?
The number of letters must be equal to the number of bytes
Thanks, but I don't need either bytes or the number of characters in the file.
 
Upvote 0
Top