此代码有效,因为它会抓取目录。但是它在所有子目录上都会出现异常,并且无法返回任何文件。这是权限问题,还是代码中有问题?
ES文件管理器显示文件,其中至少有3个包含图像的文件夹。
Sub AddImagesToMap(Dir As String)作为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
答案 0 :(得分:0)
我相信这会解决您的问题:在查看目录中是否有任何文件时,它可能是空的。我也使用了File.ListFiles函数。当文件夹为空时,它会返回未初始化的结果,如果您尝试使用未初始化的变量,则会导致异常。这是我做的:
f1 = File.ListFiles(x)
If f1.IsInitialized=False Then
f1.Initialize
End If
顺便说一句,当我试图理解File.ListFiles函数时,你在B4A上发布的这个代码是我能找到的唯一例子。谢谢,我希望这能解决你的问题。
答案 1 :(得分:0)
我用它来从我的音乐文件夹中获取我的mp3和m4a音乐文件。
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
答案 2 :(得分:0)
我知道有点晚了,但我明白了。 这是我如何获取所有文件:
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
希望我能帮助很多人提出同样的问题。
编辑:这是我的一个更好的版本。在这里,我对文件类型进行排序: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