Android Question SOLVED by Paul MEURIS - java.io.FileNotFoundException: /data/user/0/b4a.Chantier/files: open failed: EISDIR (Is a directory)

Marc DANIEL

Well-Known Member
Licensed User
Has Permission > error:
Public Sub HasPermission As Boolean
    Dim PreviousUriFileName As String
    Dim List As List
        If  File.Exists(File.DirInternal, PreviousUriFileName) Then
        PersistantURI = File.ReadString(File.DirInternal, PreviousUriFileName)
        List = ctxt.RunMethodJO("getContentResolver", Null).RunMethod("getPersistedUriPermissions", Null)
        If List.IsInitialized Then
            For Each uripermission As JavaObject In List
                Dim u As Uri = uripermission.RunMethod("getUri", Null)
                Dim temp As Object = u
                Dim s As String = temp
                If s = PersistantURI And uripermission.RunMethod("isWritePermission", Null) = True Then
                    Return True
                End If
            Next
        End If
    End If
    Return False
End Sub

Private Sub Depart
    If HasPermission Then
        FirstTime = False
        FileList_Click
    Else
        Msgbox2Async("You need to create a folder for sound recordings.", "Create new folder ?", "Yes", "", "No", Null, True)
        Wait For Msgbox_Result (Result As Int)
        If Result=DialogResponse.POSITIVE Then
            CreateNewDir
        End If
    End If
End Sub

Sub CreateNewDir
    MusicTitle.Text="Please wait while the music files are copied and downloaded"
    Storage.SelectDir(CF.Checked)
    Wait For Storage_ExternalFolderAvailable
    ' Find a file on external storage or create it
    cStoragePath = Storage.Root       
    'Installing a cookie File IN external Storage       
    FileName = "CookieFile.txt"
    CopyFileToExternalStorage(File.DirAssets, FileName, Storage.root, "")
    Log (FileName & " a été créé")
    ' Copying about ten downloaded music files to the directory chosen by the user
    Log("Download files and copy to external storage")
    ListSongName.Initialize
    ListSongName = File.ReadList(File.DirAssets, "FilesList.txt")
    For i= 0 To ListSongName.Size -1
        Log (i)
        Log(ListSongName.Size)
    NewFileName(i)=ListSongName.Get(i)
        Log(NewFileName(i))   
        FileName= NewFileName (i)
        Jb.Download("https://www.marcalaindaniel.fr/MusicBoxPlayer/"& FileName)
        Jb.GetRequest.Timeout = 100000
        wait for (Jb)Jobdone
        If Jb.Success Then
            Log("Good News ! Download was successful : " & FileName)
            IN = Jb.GetInputStream
            DestinationFile = Storage.CreateNewFile(cStoragePath, FileName)
            OUT = Storage.OpenOutputStream(DestinationFile)
            File.Copy2(IN, OUT)
            OUT.Close
            IN.Close
            Jb.release
            nf = i + 1
            MusicTitle.Text="Please wait while music file " & nf & " of 14 is downloaded"
            MusicTitle.Text = MusicTitle.Text & CRLF & FileName & " has been successfully downloaded"
        Else
            Log("Bad News ! Download was not successful : " & FileName)
        End If
    Next       
    Sleep(5000)
    MusicTitle.Text = ""
    MsgboxAsync("Congratulations, you have successfully created your directory for recordings, 14 music files have been downloaded from the web.","User Directory Selection")
    FirstTime = True
    FileList_Click
End Sub
 

Marc DANIEL

Well-Known Member
Licensed User
I'll look at this and come back here.

Bugs.jpg

Line 78 = Line 5 above​
 
Last edited:
Upvote 0

PaulMeuris

Well-Known Member
Licensed User
In the ExternalStorage library i see the same code to test the permission.
In that library the variable PreviousUriFileName is declared in the Class_Globals subroutine:
B4X:
    Public PreviousUriFileName As String = "PersistantUri"
And in the subroutine SelectDir that variable is used in the test:
B4X:
Public Sub SelectDir (UsePreviouslySelectedIfAvailable As Boolean)
    If UsePreviouslySelectedIfAvailable And File.Exists(File.DirInternal, PreviousUriFileName) Then
        PersistantUri = File.ReadString(File.DirInternal, PreviousUriFileName)
        Dim list As List = ctxt.RunMethodJO("getContentResolver", Null).RunMethod("getPersistedUriPermissions", Null)
        If list.IsInitialized Then
            For Each uripermission As JavaObject In list
                Dim u As Uri = uripermission.RunMethod("getUri", Null)
                Dim temp As Object = u
                Dim s As String = temp
                If s = PersistantUri And uripermission.RunMethod("isWritePermission", Null) = True Then
                    Log("Can use persistant uri!")
                    SetPickedDir
                    Return
                End If
            Next
        End If
    End If
    Dim i As Intent
    i.Initialize("android.intent.action.OPEN_DOCUMENT_TREE", "")
    i.PutExtra("android.content.extra.SHOW_ADVANCED", True)
    StartActivityForResult(i)
End Sub
In your code (line 2 or line 75) you declare the variable locally and set it empty by default.
B4X:
    Dim PreviousUriFileName As String
That line should not be there... and you should declare the variable outside the HasPermission subroutine.
 
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
I think you've hit the nail on the head. Thank you so much for looking at my work. I'll edit it and come back here.
 
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
I made the changes. I restarted the project without creating a folder in the external storage. There was no error message, and I was prompted to create the folder.

My problem now is that when I restart the application after creating a folder, the message continues to appear, prompting me to create a new folder, but I'll try to find the problem in my project.


B4A files

APK file
 
Last edited:
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
B4X:
    Public PreviousUriFileName As String ="PersistantUri"

My mistake: I wrote "PersistantURI" instead of "PersistantUri" and the capital letters were the cause of the bug.

Everything works fine now and when starting, the application no longer offers to create a new folder!
 
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
Yes, I know that EREL is a great guy who has already helped me on many occasions and whose work and talent I have appreciated for several years now. He has taught me a lot and continues to do so. He is the "Bill Gates" of B4X!
 
Upvote 0
Top