Android Question Intent requester and clone files?

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!


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:
 

Attachments

  • LibGdx Game(NonLibgdx version for FileTest).zip
    61.5 KB · Views: 47

Spright

Active Member
I tried to look around on all teh exapmles in this forum and they all shows the same so maybe its not a bug of mine.
It works on 4.4.4 but now on my Android 9.0 and up so it's something with updateing internal knowledge of what files actually exist.

I include a screenshot how it will look. Here i saved a a txt file with a certain name (using the SaveAs / Texteditor exmaple on the forum, the updated one). I then delete it using anything, such as the computer or Android file app for example. THen i go back i this time I will see no files in the requester, but as soon as I save a ile again now with the exact same name, it wiill pop up ghost clones of the. All the old ones with proper dates but exact same name.

If you look in android file app or comptuer or anything you will find only what truly exist, namely the file you just saved.
In B4A intent requester on android versions higher than 4.4.4 (at least 9.0 and up) you iwll ifind that the req will show a ghost copy of every sinstacne that ever exsited and was deleted, and they dates they were create is the only thing that differnetiets them apat there. If you attempt to load them, they will work as they point to the newest one.
 

Attachments

  • Screenshot.jpg
    Screenshot.jpg
    105.9 KB · Views: 35
Upvote 0

Spright

Active Member
The sad thing is that it happens on all my Samsun devices that has newer systems, even as low as Android 9.
As other apps can do proper file handling on all these devices. It's just my app (well b4a apps overall) that gets this, is there another way to get a save req in a modern safe fashion via b4a?

I can't have an app that makes ghost files because that file happened to existe before int he filesystem. This is a dealbreaker for the app to exist as it needs a save requester.
 
Upvote 0

Spright

Active Member
I have made more testing and I must take it back that it works on 4.4.4, it actualy has a similiar issue. It keeps track on the filenames correctly which newe Android version does not, but it does not track the thumbnails.

I have a feeling that whatever fixes the thumbnail update problem also fixes the filename ghosts in the newer Androids.
 
Upvote 0

Spright

Active Member
I have made all i can to make sure I have not done anything out of the ordinary. It also happens in your examples as well. It happens in all th examples I tried from the forum.

I use Samsung Note8 and Samsung A53s with updates systems, and I have tested on older devices and in-between. They all share the same priblem. Teh exception being that 4.4.4 has no rpoblem with filenames. I bet they have different ways to synchronize/update the "mediastore" or contentreoslver I am so new at this and these terms makes me confused I have to confess.

But I have made sure under these few months that I have built this app to make sure I follow all the regulations ands styels that you use., and what Google wants.
 
Upvote 0

Spright

Active Member
After tried on 20 devices I see it is visible in Download directory only on newer ones. And Kitkat and around that area has no problem even with Dwonload. So Downlaod is the only problematic folder that gives ghost files. But it happens onlevery modern device. Sadly Download is the default fodler so users might be tempted to save there and be exposed to this thign happening.
 
Upvote 0
Top