Android Question (Solved) Parse image Path into (Dir As String, File As String)

anOparator

Active Member
Licensed User
Longtime User
With the MultiPhotoPickerEx demo, ContentChooser logs the path of images in internal storage and external USB Card.
B4X:
Sub MPP_onPhotoPicked(photos As List)
'This provides USB path as: 
' Photo = /storage/BADC-14DA/dwnLD/3.jpg
    If photos <> Null Then
        If photos.Size > 0 Then
            For i= 0 To photos.Size -1
                Log($"Photo = ${photos.Get(i)}"$)     ' How to parse this into (Dir As String, File As String)?
' This provides USB path as:    
'storage
'BADC-14DA
'dwnLD
'3.jpg
    Dim s As String = photos.Get(i)
    Dim paramnum () As String = Regex.Split("\/", s)
    For Each n As String In paramnum
        Log(n)         ' Or how to parse this into (Dir As String, File As String)?
            Next
        Next
        End If
    End If
End Sub
The path might have 4, 5, or 6 parameters :
Photo = /storage/BADC-14DA/1ScrMe/3.jpg 'SD Card folder *4
Photo = /storage/emulated/0/Pictures/Messenger_Lite/16e1.0.jpg 'internal Pictures folder *6

How can I parse the path string into 2 parameters (Dir As String, File As String)?
Private myDir As String
Private myFile As String

Ending up with:
myDir = "/storage/BADC-14DA/1ScrMe"
OR
myDir = "/storage/emulated/0/Pictures/Messenger_Lite"
AND
myFile = "3.jpg"
B4X:
'  Log file
** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
** Activity (main) Resume **
Photo = /storage/BADC-14DA/dwnLD/3.jpg
storage
BADC-14DA
dwnLD
3.jpg
Photo = /storage/BADC-14DA/dwnLD/1.jpg
storage
BADC-14DA
dwnLD
1.jpg
Photo = /storage/BADC-14DA/dwnLD/2.jpg
storage
BADC-14DA
dwnLD
2.jpg
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
** Activity (main) Resume **
Photo = /storage/emulated/0/Pictures/Messenger_Lite/1.jpg
storage
emulated
0
Pictures
Messenger_Lite
1.jpg
Photo = /storage/emulated/0/Pictures/Messenger_Lite/2.jpg
storage
emulated
0
Pictures
Messenger_Lite
2.jpg
Photo = /storage/emulated/0/Pictures/Messenger_Lite/3.jpg
storage
emulated
0
Pictures
Messenger_Lite
3.jpg
I am at my wits end with this task. Thanks in advance.
 

Attachments

  • splitPath.zip
    8.2 KB · Views: 147

DonManfred

Expert
Licensed User
Longtime User
You should not try to split the result into filename and path. You´ll not get the file with it afterwards.
Use the given filename and path to open the file, Copy it whereever you want and remember this path.
 
Upvote 0

npsonic

Active Member
Licensed User
If I understand correctly you just want dir and filename from path, so here it is.
B4X:
Sub GetDir (Path As String) As String
    If Path.Contains("/") Then Return Path.SubString2(0,Path.LastIndexOf("/"))
    Return ""
End Sub
Sub GetFilename (Path As String) As String
    If Path.Contains("/") Then Return Path.SubString(Path.LastIndexOf("/") + 1)
    Return Path
End Sub
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Use the given filename and path to open the file, Copy it whereever you want and remember this path.
I am selecting multiple images at once to move into a Favorites folder, so this won't work. Thanks.
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
B4X:
Sub GetFilename (Path As String) As String
    If Path.Contains("/") Then Return Path.SubString(Path.LastIndexOf("/") + 1)
    Return Path
End Sub
I think this is the approach but when I do
B4X:
       If photos.Size > 0 Then
           For i= 0 To photos.Size -1
                Log($"Photo = ${photos.Get(i)}"$)
       CallSub(Me, "GetFilename" 'call your Sub here
' I get "java.lang.reflect.InvocationTargetException"
I think I have to put this LastIndexOf statement inside the For loop and Log it so I can copy the resulting Dir/File to a Favorites folder on each pass thru the loop.
edit: I was probably using wrong variable in place of your "Path" variable.
Sometimes writing a question can be like writing a Sub.
My B4A language not so good, thanks.
 
Last edited:
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
I knew it was in the forum somewhere , yea team :)
One Sub does it all, thanks for the pointers , npsonic et al.
https://www.b4x.com/android/forum/threads/filename-from-string.37687/ had the solution.
B4X:
Sub MPP_onPhotoPicked(photos As List)
   Private cpy, myFile, myDir As String
   If photos <> Null Then
       If photos.Size > 0 Then
           For i= 0 To photos.Size -1
               Log($"Photo = ${photos.Get(i)}"$)
               cpy = $""${photos.Get(i)}"$
               myFile = cpy.SubString(cpy.LastIndexOf("/") + 1)

               cpy = $""${photos.Get(i)}"$
               myDir = cpy.SubString2(0, cpy.LastIndexOf("/"))
               Log("show me the folder NAME - " & myDir)
               Log("show me the pic NAME - " & myFile)
               Log(" ")
                           Next
       End If
   End If
End Sub
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
** Activity (main) Resume **
Photo = /storage/emulated/0/Pictures/b4a_1shot.png
show me the folder NAME - "/storage/emulated/0/Pictures
show me the pic NAME - b4a_1shot.png
Photo = /storage/emulated/0/Pictures/b4a_2shot.png
show me the folder NAME - "/storage/emulated/0/Pictures
show me the pic NAME - b4a_2shot.png
Photo = /storage/emulated/0/Pictures/b4a_screenshot.png
show me the folder NAME - "/storage/emulated/0/Pictures
show me the pic NAME - b4a_screenshot.png
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
targetSdkVersion already updated.

Motorola E4 - 7.1.1 - API25 - device
Google Nexus 9 - 7.1.0 - API25 - emulator
Samsung Galaxy Note3 - 4.4.4 - API19 - emulator
will look around for more 7+ emulators

Just noticed the " at the end of folder name string.
-show me the folder NAME- "/storage.emulated/0/Pictures
That's my next to-do tweak.
thanks
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Getting file from Internal and SD Card, saving to internal /Data/Shared folder. Lots of testing on different devices in next days to see what if anything is wrong with the code.
B4X:
Sub MPP_onPhotoPicked(photos As List)
   Private imgPath, myFile, myDir1, myDir2 As String
   If photos <> Null Then
       If photos.Size > 0 Then
           For i= 0 To photos.Size -1
               Log($"Photo = ${photos.Get(i)}"$)
               imgPath = $""${photos.Get(i)}"$
               myFile = imgPath.SubString(imgPath.LastIndexOf("/") + 1)
               
               imgPath = $""${photos.Get(i)}"$
               myDir1 = imgPath.SubString2(0, imgPath.LastIndexOf("/"))
               myDir2 = myDir1.SubString(myDir1.LastIndexOf("chr(34)") + 2)
               
               Log("myDir1 =  " & myDir1)
               Log("myDir2 = "  & myDir2)
               Log(" pic NAME - " & myFile)
               Log(" ")
           Next
       End If
   End If
End Sub
 
Upvote 0
Top