Sub Class_Globals
Dim mContentChooser As ContentChooser
Dim mCaller as Object
Dim mstrEventName as String
Type mContentChooserData(Bitmap As B4XBitmap,RealDir As String, RealFileName As String, VirtualDir As String, VirtualFileName As String)
End Sub
Public Sub Initialize(Caller As Object,strEventName As String)
mCaller = Caller
mstrEventName = strEventName
mContentChooser.Initialize("chooser")
End Sub
Public Sub ChooseImage() As ResumableSub
mContentChooser.show("image/*", "Choose Image")
Wait For chooser_Result (Success As Boolean, Dir As String, FileName As String)
If Success = True Then
Dim strRealFilePathName As String = GetRealFilePathNameFromContentChooser(FileName) 'e.g. '/storage/emulated/0/DCIM/Camera/IMG_20201207_195412.jpg'
Dim strRealFileName As String = GetFileName(strRealFilePathName) 'e.g. 'IMG_20201207_195412.jpg'
Dim strRealPath As String = GetPath(strRealFilePathName) 'e.g. '/storage/emulated/0/DCIM/Camera'
Dim Bitmap As B4XBitmap= LoadBitmap(Dir,FileName)
Dim Data As mContentChooserData
Data.Initialize
Data.RealDir = strRealPath
Data.RealFileName = strRealFileName
Data.VirtualDir = Dir
Data.VirtualFileName = FileName
Data.Bitmap = Bitmap
Return Data
End If
Return Null
End Sub
Public Sub GetRealFilePathNameFromContentChooser(UriString As String) As String
If UriString.StartsWith("/") Then Return UriString 'If the user used a file manager to choose the image
Dim Cursor1 As Cursor
Dim Uri1 As Uri
Dim Proj() As String = Array As String("_data")
Dim cr As ContentResolver
cr.Initialize("")
If UriString.StartsWith("content://com.android.providers.media.documents") Then
Dim i As Int = UriString.IndexOf("%3A")
Dim id As String = UriString.SubString(i + 3)
Uri1.Parse("content://media/external/images/media")
Cursor1 = cr.Query(Uri1, Proj, "_id = ?", Array As String(id), "")
Else
Uri1.Parse(UriString)
Cursor1 = cr.Query(Uri1, Proj, "", Null, "")
End If
Cursor1.Position = 0
Dim res As String
res = Cursor1.GetString("_data")
Cursor1.Close
Return res
End Sub
'Will return just the Path from the given path/file name.
'ie. '/storage/emulated/0/DCIM/Camera/mypic.jpg will return '/storage/emulated/0/DCIM/Camera'
Public Sub GetPath(Path As String) As String
Dim Path1 As String
Dim L As String
If Path = "/" Then
Return "/"
End If
L = Path.LastIndexOf("/")
If L = Path.Length - 1 Then
'Strip the last slash
Path1 = Path.SubString2(0,L)
Else
Path1 = Path
End If
L = Path.LastIndexOf("/")
If L = 0 Then
L = 1
End If
Return Path1.SubString2(0,L)
End Sub
'Will return just the File Name from the given path.
'ie. '/storage/emulated/0/DCIM/Camera/mypic.jpg' will return 'mypic.jpg'
Public Sub GetFileName(FullPath As String) As String
Return FullPath.SubString(FullPath.LastIndexOf("/")+1)
End Sub
[/code}