How to get a list of all image files for a slide show

basic4android

This code works, in the sense that it crawls the directories. However it gets an exception on all the sub directories, and fails to return any files. Is this a permissions issue, or is there something wrong in my code?

ES file manager shows the files, there are at least 3 folders with images in them.

Sub AddImagesToMap(Dir As String) As Map'crawl tree for images

Dim fn As String , i As Int

Dim CRList As List, Ext As String

Try

    CRlist.initialize

    CRList=File.ListFiles(Dir)

    For i = 0 To CRlist.Size-1'jpg,png and gif

        fn=CRList.Get(i)

         Select fn     'ignore some system folders

            Case "/dev"   

            Case "/proc"

            Case "/sys"

            Case "/system"

            Case Else

                If File.IsDirectory(Dir,CRList.Get(i)) Then

                    Log("Dir:"&fn)

                    AddImagesToMap(fn)

                Else

                    Log(fn)

                    Ext= common.FileExt(fn)

                    If ext.ToLowerCase="jpg" OR ext.ToLowerCase="png" OR ext.ToLowerCase="gif" Then

                        Imagelist.Put(fn, Dir)

                    End If

                End If
        End Select

    Next

Catch

    Log ("error:"&Dir)

End Try

End Sub

Best Answer

I believe this will fix your problem: when looking to see if a directory has any files in it, it may be empty. I too used the File.ListFiles function. When a folder is empty it returns an uninitialized result, which if you try to use an uninitialized variable will cause an exception. Here's what I did:

f1 = File.ListFiles(x)
If f1.IsInitialized=False Then
    f1.Initialize
End If

By the way, when I was trying to understand the File.ListFiles functions this code you posted on B4A was the only example I could find. So thanks, and I hope this solves your problem.

Related Topic