Android Question Can't create directory or file

MarcRB

Active Member
Licensed User
Longtime User
Hi I do have two tablets.

1. Asus Zenpad Z380M 8inch
2. Lenovo Tab4 8inch (Android 8.1.0)

Only at the Lenovo I can't create a directory or file in /storage/emulated/0
(= File.DirRootExternal)
This small test fails on the Lenovo:

B4X:
Msgbox(File.DirRootExternal,"Show location of rootext.")
  
File.MakeDir(File.DirRootExternal, "DataHelloWorld")
If File.Exists(File.DirRootExternal, "DataHelloWorld") Then
     Msgbox("Folder created. Yes!","Test")
Else
      Msgbox("Folder is not created. Why?","Test") 'this is the result
End If

I allready added the next line to the manifest.
B4X:
AddManifestText(<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>)

Outside my B4A app I can create a directory in that location using a commercial filemanager app.
With my B4A app I can succesfully check if the directory exists, but it is still not possible to write anything in that folder.

Extra info:
There is enough diskspace, no warning or error and no sd-card is inserted in the device.
Also there is no cabled connetion with a computer.
I can use File.DirDefaultExternal instead, and that works fine. The folder is created and writebale inside, but I want the data stay intact after removing or re-Installing the app. That is not possible at that location. Therefore I try to use rootexternal.

Can anybody help me solving this?
 

MarcRB

Active Member
Licensed User
Longtime User
It is solved now. By adding a runtime permission
Did you read the tutorial runtime permissions?
Yes is found that, but because of this line
The explanation for this is that GetSafeDirDefaultExternal doesn't require any permission on Android 4.4+ (API 19) and requires the WRITE_EXTERNAL_STORAGE on older versions. The code above adds the permission to older devices.
, I tought it was only for older tablets with this problem and it wouldn't help me. But I was wrong it was very helpfull even for Android 8.1.

B4X:
Sub Process_Globals
      Private rp As RuntimePermissions
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If Permission = rp.PERMISSION_WRITE_EXTERNAL_STORAGE Then
        If Result = True Then

           ' *** here my file writing stuf

            If File.Exists(modCode.gstrDataFolder, "config.ini") = False Then
                StartActivity("actLicense") 
            Else
                initLogin 
            End If
        Else
                Msgbox("No permission for writing files.","No permission for writing files")
        End If
    End If
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime = True Then
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    End If
End Sub
 
Upvote 0
Top