Android Question [Solved] Makedir failure

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

I can't believe I am asking this question, it should be simple. Below a piece of code in Activity Create, checks for a dir, makes dir. simple. But it is not making the dir.
After chasing my tail for a couple of hours I copied the code from another App where it is working and changed the dir name, no good.

If someone can see what I've done wrong it would be much appreciated.

Makedir not working:
        'Permission to READ/WRITE External Memory
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        '....  and then
        If Result Then     'The HepFile is copied to an external directory where it can transferred/printed etc.
            'Copy "Help" file to the "ABT" folder
            If File.exists(File.DirRootExternal&"/ABT/","") = False Then File.MakeDir(File.DirRootExternal&"/ABT/","")
            File.Copy(File.DirAssets,"helpfile.htm",File.DirRootExternal&"/ABT/","ABTHelp.htm")
        End If


The error log.

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 586 (Main)
java.io.FileNotFoundException: /storage/emulated/0/ABT/ABTHelp.htm: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:496)
at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:448)
at anywheresoftware.b4a.objects.streams.File.Copy(File.java:341)
at horsetrailer.B4A.AntennaBearingTool.main$ResumableSub_Activity_Create.resume(main.java:2087)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1704)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7814)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
If someone can see what I've done wrong it would be much appreciated.
Don't get rattled Roger. Your code looks good, unless you are using a B4XPages project. I tested it on a OS 8.1 device with manifest line:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
The first thing you might want to do is uninstall and reinstall the app , then check your OS version
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
This works for me
B4X:
    ' DON'T TARGET ABOVE API 28 FOR THE TIME BEING AS WE NEED TO WORK OUT STORAGE ACCESS
    ' Need to get all the first time things done before Wait For as FirstTime is no longer set on re-entry after a wait
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)   
    Log($"PERMISSION_WRITE_EXTERNAL_STORAGE = ${Result}"$)   

    If Not(File.IsDirectory(File.DirRootExternal, IdeDir)) Then
        File.MakeDir(File.DirRootExternal, IdeDir)
    End If
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Don't get rattled Roger. Your code looks good, unless you are using a B4XPages project. I tested it on a OS 8.1 device with manifest line:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
The first thing you might want to do is uninstall and reinstall the app , then check your OS version

Mahares thanks for the reply.
Device Galaxy A70 Android 10. Uninstalled/installed many times.
Working my way through the replies.
Manifest:

'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136

AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

SetActivityAttribute(HelpActiv, android:screenOrientation, "landscape")

CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.

AddManifestText( <permission
android:name="$PACKAGE$.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>)

AddApplicationText( <meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" /> )

AddApplicationText(<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyAn4mAf8Nqha2H5oHHYvbCx9Ge37PADBYY"/>)

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

AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
MakeDir looks wrong. It probably 'seems' to work in the other app because the folder already exists. Try:

File.MakeDir(File.DirRootExternal, "ABT")
agraham,
When I said works I meant that creating the Dir works. I have uninstalled and deleted the ABT folder, restarted the device, restarted the IDE. No success.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
This works for me
B4X:
    ' DON'T TARGET ABOVE API 28 FOR THE TIME BEING AS WE NEED TO WORK OUT STORAGE ACCESS
    ' Need to get all the first time things done before Wait For as FirstTime is no longer set on re-entry after a wait
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean) 
    Log($"PERMISSION_WRITE_EXTERNAL_STORAGE = ${Result}"$) 

    If Not(File.IsDirectory(File.DirRootExternal, IdeDir)) Then
        File.MakeDir(File.DirRootExternal, IdeDir)
    End If
Sorry agraham but I get an Undeclared Variable error for IdeDir.
 
Last edited:
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Add this to your manifest. This should work for you since you are using OS 10
B4X:
SetApplicationAttribute(android:requestLegacyExternalStorage, true)

Mahares,
Fixed it. Tested on A70 android 10 and Note 3 Android 5. That should cover most options.

Many Thanks again Mahares.

In a perfect world, "Permissions" would be transparent to the developer. If the Android System wants the user to give permission, the Android System should be requesting it.
Just my opinion of course. :)
 
Upvote 0
Top