'Creates folder in the destination folder selected
'Copies file from DirInternal
Sub FiletoExtStorage(DirName As String,FileToCopy As String)
Dim inpstr As InputStream = File.OpenInput(File.DirInternal,FileToCopy) 'Create an Inputstream from the Sourcefile to copy
Dim DirName1 As ExternalFile = Storage.FindDirOrCreate(Storage.Root, DirName) 'creates the folder
Dim destfile As ExternalFile = Storage.CreateNewFile(DirName1,FileToCopy) 'create the file
Dim os As OutputStream = Storage.OpenOutputStream(destfile) 'Create an Outputstream to the destfile
File.Copy2(inpstr,os) 'Copy file
inpstr.Close 'Close inputstr
os.Close 'Close Outputstream
End Sub
Sub SetTestFiles
FileName1= "RodTest1.txt"
File.WriteString(File.DirInternal, FileName1, "jaklsdjalksdjalskdjasld")
Filename2= "RodTest2.txt"
File.WriteString(File.DirInternal, Filename2, "qwertyqwerty")
End Sub
Sub btnTest_Click
SetTestFiles
Storage.SelectDir(True)
Wait For Storage_ExternalFolderAvailable
FiletoExtStorage("Terminations",FileName1) ' Directory and File to Send
FiletoExtStorage("Terminations",Filename2) ' Directory and File to Send
End Sub
Public Sub FindDirOrCreate(Parent As ExternalFile, Name As String) As ExternalFile
Dim f As ExternalFile = FindFile(Parent, Name)
If f.IsInitialized = False Then
Return DocumentFileToExternalFile(Parent.Native.RunMethod("createDirectory", Array(Name)))
Else
Return f
End If
End Sub
Android - B4A Multiplatform - B4X
Sub SaveAs (Source As InputStream, MimeType As String, Title As String) As ResumableSub
Dim intent As Intent
intent.Initialize("android.intent.action.CREATE_DOCUMENT", "")
intent.AddCategory("android.intent.category.OPENABLE")
intent.PutExtra("android.intent.extra.TITLE", Title)
intent.SetType(MimeType)
StartActivityForResult(intent)
Wait For ion_Event (MethodName As String, Args() As Object)
If -1 = Args(0) Then 'resultCode = RESULT_OK
Dim result As Intent = Args(1)
Dim jo As JavaObject = result
Dim ctxt As JavaObject
Dim out As OutputStream = ctxt.InitializeContext.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null)))
File.Copy2(Source, out)
out.Close
Return True
End If
Return False
End Sub
Sub StartActivityForResult(i As Intent)
Dim jo As JavaObject = GetBA
ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
jo.RunMethod("startActivityForResult", Array(ion, i))
End Sub
Sub GetBA As Object
Dim jo As JavaObject = Me
Return jo.RunMethod("getBA", Null)
End Sub
HOW TO USE (B4XPages)
Private Sub Button1_Click
File.WriteString(File.DirInternal, "test.txt", "test") 'just for the example.
Wait For (SaveAs(File.OpenInput(File.DirInternal, "test.txt"), "application/octet-stream", "test.txt")) Complete (Success As Boolean)
Log("File saved successfully? " & Success)
End Sub
HOW TO USE (SIMPLE)
Sub GetBA As Object
Dim jo As JavaObject
Dim cls As String = Me
cls = cls.SubString("class ".Length)
jo.InitializeStatic(cls)
Return jo.GetField("processBA")
End Sub
Android - B4A iOS - B4i Desktop - B4J Multiplatform - B4X
' READ DOCUMENT
Public Sub getDocument(Coleccion As String) As ResumableSub
Dim link As String = $"https://firestore.googleapis.com/v1/projects/{projectId}/databases/(default)/documents/${Coleccion}"$
Dim okHttp As HttpJob
okHttp.Initialize("HTTP", Me)
okHttp.Download(link & "info_chats")
Wait For (okHttp) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
Dim jsonString As JSONParser
jsonString.Initialize(j.GetString)
Dim map As Map = jsonString.NextObject
Return map
Else
Log(j.ErrorMessage)
End If
End Sub
Example:
Wait For (getDocument("server/names")) Complete(m As Map)
' VALUES:
Public Sub patchValues(Token As String, collection As String, Campos As Map) As ResumableSub
Dim url As String = $"https://firestore.googleapis.com/v1/projects/{projectId}/databases/(default)/documents${collection}"$'?documentId=${Documento}"$
Dim Json As JSONGenerator
Json.Initialize(CreateMap("fields":Campos))
Dim j As HttpJob : j.Initialize("",Me)
Log(Json.ToString)
j.PatchString(url,Json.ToString)
j.GetRequest.SetHeader("Authorization","Bearer " & Token)
j.GetRequest.SetContentType("application/json")
Wait For (j) JobDone(j As HttpJob)
Return GenerateResult(j)
End Sub
' Example:
Wait For (patchValues(UserTokenId, "info_chats/id_group_chat1", CreateMap("name" : CreateMap("stringValue": "Paolo")))) Complete(m As Map)
' Create document:
Public Sub createDocument(Token As String, collection As String, Documento As String, Campos As Map) As ResumableSub
Dim url As String = $"https://firestore.googleapis.com/v1/projects/{projectId}/databases/(default)/documents/${collection}?documentId=${Documento}"$
Dim json As JSONGenerator
json.Initialize(CreateMap("fields":Campos))
Dim j As HttpJob : j.Initialize("",Me)
Log(json.ToString)
j.PostString(url,json.ToString)
j.GetRequest.SetHeader("Authorization","Bearer " & Token)
j.GetRequest.SetContentType("application/json")
Wait For (j) JobDone(j As HttpJob)
Return GenerateResult(j)
End Sub
' Example:
Wait For (Firestore.createDocument(UserTokenId, "info_chats","Profile", CreateMap("name" : CreateMap("stringValue": "Paolo")))) Complete(m As Map)
' Deletedocument:
Public Sub deleteDocument(Token As String, collection As String) As ResumableSub
Dim url As String = $"https://firestore.googleapis.com/v1/projects/{projectid}/databases/(default)/documents/${collection}"$'?documentId=${Documento}"$
Dim j As HttpJob : j.Initialize("",Me)
j.Delete(url)
j.GetRequest.SetHeader("Authorization","Bearer " & Token)
Wait For (j) JobDone(j As HttpJob)
Return GenerateResult(j)
End Sub
'EXAMPLE:
Wait For (Firestore.deleteDocument(UserTokenId, "info_chats/Profile")) Complete(m As Map)
' SUJESTED USE OF CODE WITH MAP
Private Sub GenerateResult(j As HttpJob) As Map
Dim response As String = ""
If j.Success Then
response = j.GetString
Log(j.GetString)
Else
response = j.ErrorMessage
End If
Dim parser As JSONParser
parser.Initialize(response)
Dim tmp_result As Map = parser.NextObject
tmp_result.Put("success",j.Success)
j.Release
Return tmp_result
End Sub
Android - B4A iOS - B4i Desktop - B4J Multiplatform - B4X
Sub AnimateDialog (dlg As B4XDialog, FromEdge As String)
Dim base As B4XView = dlg.Base
Dim top As Int = base.Top
Dim left As Int = base.Left
Select FromEdge.ToLowerCase
Case "bottom"
base.Top = base.Parent.Height
Case "top"
base.Top = -base.Height
Case "left"
base.Left = -base.Width
Case "right"
base.Left = base.Parent.Width
End Select
base.SetLayoutAnimated(300, left, top, base.Width, base.Height)
End Sub
' HOW TO USE:
Sub Globals
Private dialog As B4XDialog
Private xui As XUI
End Sub
Sub Activity_Create(FirstTime As Boolean)
dialog.Initialize(Activity)
dialog.Title = "test"
End Sub
Sub Activity_Click
Dim rs As Object = dialog.Show("aaa", "Ok", "Not Ok", "")
AnimateDialog(dialog, "right")
Wait For (rs) Complete (Result As Int)
If Result = xui.DialogResponse_Positive Then
'...
End If
End Sub
Android - B4A iOS - B4i Desktop - B4J Multiplatform - B4X
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://www.google.com")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
End If
j.Release
Android - B4A iOS - B4i Desktop - B4J Multiplatform - B4X
Sub DownloadQuote
Dim j As HttpJob
j.Initialize("", Me) 'name is empty as it is no longer needed
j.Download("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
'The result is a json string. We parse it and log the fields.
Dim jp As JSONParser
jp.Initialize(j.GetString)
Dim quotes As List = jp.NextArray
For Each quot As Map In quotes
Log("Title: " & quot.Get("title"))
Log("Content: " & quot.Get("content"))
Next
End If
j.Release
End Sub
Android - B4A iOS - B4i Desktop - B4J Multiplatform - B4X
Sub Activity_Create(FirstTime As Boolean)
DownloadMany(Array("http://www.google.com", "http://duckduckgo.com", "http://bing.com"))
End Sub
Sub DownloadMany (links As List)
For Each link As String In links
Dim j As HttpJob
j.Initialize("", Me) 'name is empty as it is no longer needed
j.Download(link)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log("Current link: " & link)
Log(j.GetString)
End If
j.Release
Next
End Sub
Android - B4A iOS - B4i Desktop - B4J Multiplatform - B4X
Sub DownloadImage(Link As String, iv As ImageView)
Dim j As HttpJob
j.Initialize("", Me)
j.Download(Link)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
iv.Bitmap = j.GetBitmap
End If
j.Release
End Sub
Android - B4A iOS - B4i Desktop - B4J Multiplatform - B4X
Sub DownloadAndSaveFile (Link As String)
Dim j As HttpJob
j.Initialize("", Me)
j.Download(Link)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "filename.dat", False)
File.Copy2(j.GetInputStream, out)
out.Close ------ very important
End If
j.Release
End Sub
Android - B4A iOS - B4i Desktop - B4J Multiplatform - B4X
server.Initialize(Main, "FTPServer")
server.SetPorts(51041, 51042, 51142)
server.AddUser("Test", "test") 'user name and password.
server.BaseDir = File.DirRootExternal
server.Start
Android - B4A Multiplatform - B4X
files are included
server.Initialize(Main, "FTPServer")
server.SetPorts(51041, 51042, 51142)
server.AddUser("Test", "test") 'user name and password.
server.BaseDir = File.DirRootExternal
server.Start
Android - B4A iOS - B4i Desktop - B4J Multiplatform - B4X
The sample shows how to delete a file, previously shows a dialog with 3 options the "yes" return -1, the rest cancel the dialog
Private Sub swbtnDelete_Click
Dim confirmDialog As B4XDialog
confirmDialog.Initialize(Root)
Wait For (confirmDialog.Show("Sure?", "YES", "NO", "CANCEL")) Complete (Result As Int)
If Result = xui.DialogResponse_Positive Then
Log(Result)
File.Delete(xui.DefaultFolder & "privateCABINET" ,listofpics.Get(currentPreviewIndexfile))
pnlPreview.Visible=False
End If
End Sub
Android - B4A iOS - B4i Desktop - B4J Multiplatform - B4X
Sub ShowFile_Chooser (FilePathCallback As Object, FileChooserParams As Object)
cc.Initialize("CC")
cc.Show("*/*", "Choose File")
Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
Dim jo As JavaObject = Me
If Success Then
Log(FileName)
File.Copy(Dir, FileName, Starter.Provider.SharedFolder, "TempFile")
jo.RunMethod("SendResult", Array(Starter.Provider.GetFileUri("TempFile"), FilePathCallback))
Else
jo.RunMethod("SendResult", Array(Null, FilePathCallback))
End If
End Sub
Android - B4A