Android Question Files in the user folder (OS12.0-API 31)

Sergey_New

Well-Known Member
Licensed User
Longtime User
The user created a folder "MyFolder" and placed several text files with the extension *.ged in it.
How can I programmatically get a list of these files without using the file manager built into the device?
I believe that they need to be copied to a safe place and worked with them there.
I don’t know yet how this can be done. Please help.
 

arjian

Member
Licensed User
Longtime User
B4X:
    Dim ListOfFiles As List,i As Int
    Dim FileName As String
    
    ListOfFiles.Initialize
    
    ListOfFiles.AddAll(File.ListFiles(File.DirDefaultExternal & "/Downloads" ))  '-- set MyFolder adress base on it's location
    
    For i=0 To ListOfFiles.Size-1
        FileName=ListOfFiles.Get(i)
        If FileName.EndsWith(".ged") Then
            '-- code
        End If
    Next
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
arjian, an exception occurs at the line:
B4X:
ListOfFiles.AddAll(File.ListFiles(File.DirDefaultExternal & "/Downloads" ))
java.io.IOException: /storage/emulated/0/Android/data/sv.bystrovzorov/files/Download is not a folder.
On my emulator the folder is called Download, but in both cases the error repeats.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You do not have access to this folder.
Check
and
 
Upvote 0

arjian

Member
Licensed User
Longtime User
arjian, an exception occurs at the line:
B4X:
ListOfFiles.AddAll(File.ListFiles(File.DirDefaultExternal & "/Downloads" ))
java.io.IOException: /storage/emulated/0/Android/data/sv.bystrovzorov/files/Download is not a folder.
On my emulator the folder is called Download, but in both cases the error repeats.
first of all you need have access to read files
the second you need to set your folder location correctly

for example i created a folder (MyFolder) on my phone so now i can get list of files in that folder with this code



B4X:
Dim ListOfFiles As List,i As Int
    Dim FileName As String
    
    
    ListOfFiles.Initialize
    ListOfFiles.AddAll(File.ListFiles("/storage/emulated/0/MyFolder" ))
    
    For i=0 To ListOfFiles.Size-1
        FileName=ListOfFiles.Get(i)
        Log("file " & (i+1) & " : " & FileName)
    Next
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
first of all you need have access to read files
The manifest has read permission set. The files are not readable.
AddManifestText(
<uses-sdk android:minSdkVersion="28" android:targetSdkVersion="33"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo.Light")
SetActivityAttribute(ListEvents, android:excludeFromRecents, true)
SetApplicationAttribute(android:requestLegacyExternalStorage, true)
AddActivityText(Main,
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
)
AddManifestText(<permission
android:name="$PACKAGE$.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>)
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)

AddApplicationText(
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
)

AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)

AddApplicationText(
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="$PACKAGE$.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
)

CreateResource(xml, provider_paths,
<files-path name="name" path="shared" />
)
CreateResourceFromFile(Macro, Themes.LightTheme)
CreateResource(values-v21, theme.xml,
<resources>
<style name="LightTheme" parent="@android:style/Theme.Material.Light">
<item name="android:textAllCaps">false</item>
</style>
</resources>
)
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Thank you!
Unfortunately, the files cannot be read on the emulator. If you create a “Media” subfolder in the MyFolder folder, it will be displayed. This means there is access to the MyFolder folder, but not to the files.
An exception occurs on the Samsung S7 device:
Error occurred on line: 47 (Main)
java.lang.RuntimeException: Object should first be initialized (List).
 
Upvote 0

arjian

Member
Licensed User
Longtime User
Thank you!
Unfortunately, the files cannot be read on the emulator. If you create a “Media” subfolder in the MyFolder folder, it will be displayed. This means there is access to the MyFolder folder, but not to the files.
An exception occurs on the Samsung S7 device:
Error occurred on line: 47 (Main)
java.lang.RuntimeException: Object should first be initialized (List).
Copy a random file to MyFolder then run app again
By the way don't forget to initialize the list variable
There's a line in code (ListofFilles.initialize) for that
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Для этого в коде есть строка (ListofFilles.initialize).
В вашем случае это не так. В строке 43 есть: ListOfFiles.Initialize.
Я скопировал файл еще раз. Никаких изменений нет.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Isn't there already a solution to this problem?
 
Upvote 0
Top