0

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

GS106
  • 1
  • 1
  • 2

3 Answers3

0

I know that's a bit late, but I figured it out. Here's how I can get all files:

Sub AddImagesToMap(Dir As String) 'crawl tree for files
Dim fn As String , i As Int
Dim CRList As List
Try
    CRList.initialize
    CRList=File.ListFiles(Dir)

    For i = 0 To CRList.Size-1'jpg,png and gif
        fn=CRList.Get(i)
                If File.IsDirectory(Dir,CRList.Get(i)) Then
                    Log("Dir: "&fn)
                    AddImagesToMap(Dir & "/" & fn)
                Else
                        Log(fn & " - " & Dir)
                End If
    Next
Catch
    Log ("Error: "&Dir)
End Try
End Sub

Hope I helped a lot of people with the same question.

EDIT: Here's a better version of mine. Here I sort the file types:

Sub AddImagesToMap(Dir As String)
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
        fn=CRList.Get(i)

                If File.IsDirectory(Dir,CRList.Get(i)) Then
                    AddImagesToMap(Dir & "/" & fn)
                Else

                If fn.Contains(".") Then
                Ext=fn.SubString(fn.LastIndexOf("."))
                End If

                Select Case Ext

                Case ".jpg"
                    Log(fn & " - " & Dir)

                Case ".png"
                    Log(fn & " - " & Dir)

                End Select

                End If
    Next
Catch
    Log ("Error: "&Dir)
End Try
End Sub
Fusseldieb
  • 1,324
  • 2
  • 19
  • 44
0

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.

0

I use this to get my mp3 amd m4a music files from my music folder.

Sub Activity_Create(FirstTime As Boolean)
  ListView1.Initialize("ListView1")
  Dim GD As GradientDrawable
  GD.Initialize("TR_BL", Array As Int(Colors.Gray, Colors.LightGray))
  Activity.Background = GD
  ListView1.ScrollingBackgroundColor = Colors.Transparent
  Dim Bitmap1 As Bitmap
  Bitmap1.Initialize(File.DirAssets, "button.gif")

  ListView1.SingleLineLayout.ItemHeight = 50dip
    ListView1.SingleLineLayout.Label.TextSize = 20
    ListView1.SingleLineLayout.Label.TextColor = Colors.Blue
    ListView1.SingleLineLayout.Label.Gravity = Gravity.LEFT
  ListView1.FastScrollEnabled = True

  root=File.DirRootExternal
  filePath = root & "/music/"

  FindFolder(filePath,"")

  Activity.AddView(ListView1, 0, 0, 100%x, 100%y)

End Sub

Sub FindFolder(myPath As String, subfolder As String) As String
  Dim fileList As List
  Dim i As Int
  Dim p,f As String

  fileList = File.ListFiles(myPath)
  fileList.Sort(True)

  For i = 0 To fileList.Size-1
    p = myPath
    f = fileList.Get(i)

    If File.IsDirectory(p, f) Then
      p = p & "/" & f
      p = FindFolder(p, subfolder)  '<---recursive
    Else
      If f.EndsWith("m4a") OR f.EndsWith("mp3") Then
        album = p.SubString(p.LastIndexOf("/")+1) 'treat folders names as artist or album
        song = f.SubString2(0,f.LastIndexOf(".")) 'treat files as musc remoce extension name
        ListView1.AddSingleLine(album & " : " & song)
      End If
    End If
  Next

End Sub