Spright
Active Member
I'm gettting weird results while using the standard intent requester?
I have made as clean and tiny test as I could now, I picked out libgdx just to see that it works the same and it does.
The beheviour is that I can save files fine, but if I manually delete the files and relaunch the app and save a file with the same name,
i will see *two* files with the exact same name the third time I launch it.
When you inspect what files really exist using the computer or Android file app you will see that it containts nothign but the true files,, none of those clones.
But once back in the requester the neext time they will all show up (And point to the original file lik as if they do exist).
As I understand this is some kind of synchronisation issue and I have google for that for days now without actually finding the cause?
Any help would be much appriciated!
I have made as clean and tiny test as I could now, I picked out libgdx just to see that it works the same and it does.
The beheviour is that I can save files fine, but if I manually delete the files and relaunch the app and save a file with the same name,
i will see *two* files with the exact same name the third time I launch it.
When you inspect what files really exist using the computer or Android file app you will see that it containts nothign but the true files,, none of those clones.
But once back in the requester the neext time they will all show up (And point to the original file lik as if they do exist).
As I understand this is some kind of synchronisation issue and I have google for that for days now without actually finding the cause?
Any help would be much appriciated!
B4X:
' Test platform for a LibGDX game
' Save uses a standard intent filerequester
' After you saved a file, go to Android fil manager and delete it, and check that no files exist wirth that name.
' Now run this test again, and pick the same name, and save it.
' Now in the Android file app you will see the true filessystem, one file.
' Launch the app and requester a third time and you will see two (or as many times as you do this) files with the same name and different dates.
' How synchronize things so the requester will see what ectually exist?
#Region Init
#Region Project Attributes
#FullScreen: True
#IncludeTitle: False
#ApplicationLabel: Save requester file test
#VersionCode: 1
#VersionName: F
#SupportedOrientations: Portrait
#CanInstallToExternalStorage: False
#End Region
#End Region
Sub Process_Globals
Private ion As Object
End Sub
Sub Globals
Dim jo As JavaObject, rp As RuntimePermissions
Dim sys As Phone, Surface As View, awidth As Int, aheight As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
awidth = Activity.Width : aheight = Activity.Height
' save test
' rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Not needed with intent
Save ' d
End Sub
Sub ion_Event (MethodName As String, Args() As Object)
Log ("ion")
Dim result_ok As Int = 0 ' Default for 'false' or 'failure'
If MethodName = "Result" Then
Dim resultCode As Int = Args(0)
Dim ResultIntent As Intent = Args(1)
If resultCode = result_ok Then
Dim contentUri As String = ResultIntent.GetData
Log("Content URI: " & contentUri)
' Update media store?
Else
Log("Activity result canceled or encountered an error.")
End If
End If
End Sub
Sub StartActivityForResult(iiii As Intent)
Dim jo As JavaObject, cls As String = Me
cls = cls.SubString("class ".Length)
jo.InitializeStatic(cls)
jo = jo.GetField("processBA")
ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
jo.RunMethod("startActivityForResult", Array(ion, iiii))
End Sub
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
If Permission = rp.PERMISSION_WRITE_EXTERNAL_STORAGE Then
If Result = True Then
ShowToastAt(0,0, "Permission granted.")
Else
ShowToastAt(0,0, "Permission denied.")
End If
End If
End Sub
Sub Save
Dim contentUri As String, success As Boolean
Dim in As InputStream = File.OpenInput(File.DirAssets, "avatar.png")
Wait For (SaveAs(in, "image/png", "avatar.png")) Complete (contentUri As String)
If contentUri <> "" Then
success = True
Log("Succes To get URI")
Log(contentUri)
End If
If success Then
ShowToastAt(0,0, "Image saved")
Else
ShowToastAt(0,0, "Image not saved")
End If
sys.HideKeyboard(Activity) ' Root in B4XPages
End Sub
Public Sub SaveAs(Source As InputStream, MimeType As String, Title As String) As ResumableSub
Log("saver")
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
ctxt.InitializeContext
Dim out As OutputStream = ctxt.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null)))
Try
File.Copy2(Source, out)
out.Close
Source.Close
Dim contentUri As String = result.GetData
Return contentUri
Catch
Log(LastException.Message)
End Try
End If
Return ""
End Sub
' ---------------------------------------------------------------------------------------------
Sub ShowToastAt(x As Int, y As Int, text As String)
Dim duration As Int = 0, ctxt As JavaObject, toastJO As JavaObject
ctxt.InitializeContext
toastJO = toastJO.InitializeStatic("android.widget.Toast").RunMethod("makeText", Array(ctxt, text, duration))
toastJO.RunMethod("setGravity", Array(Bit.Or(Gravity.CENTER, Gravity.CENTER), x, y))
toastJO.RunMethod("show", Null)
End Sub
B4X: