Android Question How access Download folder with GetSafeDirDefaultExternal

DataProtec

Member
Licensed User
I need Access the Download Folder only as Read, not as write, with
B4X:
If (File.Exists(File.DirRootExternal , "/Download/text.txt")) Then  '*** Works, file exist, but can't open with permissinon problems
'*** correct Path = File  /storage/emulated/0
'** get - java.io.FileNotFoundException: /storage/emulated/0/Download/conf.dpn (Permission denied)

If (File.Exists(rp.GetSafeDirDefaultExternal(""), "/Download/Text.txt")) Then  '*** won't work, is other Path
'*** wrong path =  /storage/emulated/0/Android/data/PST.Printer/files
In Manifest i have
AddManifestText(<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="19" />)

How can i solve this, i have readed all i could find, but nothing gives me a help[/CODE]
 

Sasuke Sama

Active Member
Licensed User

DataProtec

Member
Licensed User
Finaly my code as i need and with DirRootExternal and manifest targetSdkVersion="18" Works fine
But in case we must upgrade to 26 or better 28 won't work, Permission denied
B4X:
Dim DirFile As String
        DirFile = rp.GetSafeDirDefaultExternal("") '** should point to Donwload Folder
        'DirFile = File.DirRootExternal
 
        If (File.Exists(DirFile , "conf.dpn")) Then
         
        If (File.Exists(File.DirInternal, "conf.smc")) Then
            File.Delete(File.DirInternal, "conf.smc")
            Log("File deleted")
        End If
         
            Private SecString As String
            Dim ProgID As String
            ProgID = File.ReadString(DirFile, "conf.dpn")
            SecString = ProgID
            Log("Prog ID " & ProgID)
         
            File.WriteString(File.DirInternal, "conf.smc", SecString)
            'File.Delete(File.DirRootExternal, "/Download/conf.dpn")
        Else
            Log("File does not exist")
        End If
 

DataProtec

Member
Licensed User
I have read both, but do not give me the answer i need
I like to Access the download folder where you sabe your downloads.
The final soloution should when i tap on a downloaded file which is assosiated with my App to open with this, but i don't know how i can get the path in my programm. If i tap on a downloaded file my App comes up, but need to get the Path of the file in case the Download Folder is in different places of the Devices
 

DonManfred

Expert
Licensed User
I have read both, but do not give me the answer i need
Not true.
As you are accessing File.DirRootExternal you are not accessing the external sdcard but the 2nd, internal, sdcard. To accesss this on higher Android you need to use Runtimepermission.

@Sasuke Sama wrote:
If you're trying to access the internal device storage then you just need to give the read permission with rp
See
https://www.b4x.com/android/forum/threads/runtime-permissions-android-6-0-permissions.67689/
That´s exactly the answer you need!

Again, i suggest you to read and understand this thread: android.jar / targetSdkVersion / minSdkVersion
Bookmark this thread. you should review the thread again for newer sdks...

If (File.Exists(File.DirRootExternal , "/Download/text.txt")) Then
To get this to work you need to use Runtimepermissions library in your app and correctly Request permission for it. See the tutorial.
 

DataProtec

Member
Licensed User
I've read the Threads a few times and also watched the video, but could not get together to make it working.
I did again what DonManfred saied and no seems working, in my Tes Enviorment
Thanks
 

Sasuke Sama

Active Member
Licensed User
I've read the Threads a few times and also watched the video, but could not get together to make it working.
I did again what DonManfred saied and no seems working, in my Tes Enviorment
Thanks
B4X:
    rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If     Result=True Then

    Else
        Msgbox("Please grant the required permission in order to run the app","")
    Activity.Finish
    End If
just put this code in the activity create sub
 
Top