Android Question Find DCIM folder

AlpVir

Well-Known Member
Licensed User
Longtime User
Is there a function that allows you to find the DCIM folder intended to contain the photos (in the Camera subfolder)?
Whether this is in the main memory or in the removable external memory.
On all occasions !
The function below produces unreliable results:
B4X:
Sub GetExternalStoragePublicDirectory (name As String) As String
   Dim jo As JavaObject
   jo.InitializeStatic("android.os.Environment")
   Dim path As Object = jo.RunMethod("getExternalStoragePublicDirectory", Array(name))
   If path = Null Or File.Exists(path, "") = False Then
     Log("No such folder.")
     Return ""
   End If
   Return path
End Sub

Don Manfred's Storage 1.0 Library also fails in some cases.
 

ronell

Well-Known Member
Licensed User
Longtime User
you need runtime permission to access external storage
B4X:
rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
I assumed that write / read permission was needed.
In manifest :
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="29"/>
and
AddPermission("android.permission.WRITE_EXTERNAL_STORAGE")
AddPermission("android.permission.READ_EXTERNAL_STORAGE")
Actually my project is a bit more complicated.
I would like to identify which is the most recent photo, looking for it both in the DCIM/Camera folder of the main memory and in the DCIM/Camera folder of the MicroSD.
It is in this second case that I cannot identify this folder which, in 2 cases examined by me, can be:
/storage/7AA1-6DD1/DCIM/camera
or
/storage/MicroSD/DCIM/camera
As for the search for the most recent photo, the following sub should work (not yet tested)
B4X:
Sub MostRecentFile2(dir1 As String, dir2 As String) As String
    Dim latest As String
    Dim t As Long = 0
    Log("MostRecentFile2 in " & dir1)
    If File.ListFiles(dir1).Size>0 Then
        For Each f As String In File.ListFiles(dir1)
            If Not(File.IsDirectory(dir1,f)) Then
                If File.LastModified(dir1,f)>t Then
                        latest = f
                        Log("1 MostRecentFile Latest=" & latest)
                        t = File.LastModified(dir1,f)
                End If
            End If
        Next
    End If
    Log("MostRecentFile2 in " & dir2)
    If File.ListFiles(dir2).Size>0 Then
        For Each f As String In File.ListFiles(dir2)
            If Not(File.IsDirectory(dir2,f)) Then
                If File.LastModified(dir2,f)>t Then
                        latest = f
                        Log("2 MostRecentFile Latest=" & latest)
                        t = File.LastModified(dir2,f)
                End If
            End If
        Next
    End If
    Return latest
End Sub
 
Upvote 0
Top