Android Question SaveAs on existing file

JohnC

Expert
Licensed User
Longtime User
A workaround would be to create a sub that would:
1) First check if the desired file already exists.
2) If so, then delete it before doing SaveAs.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1) First check if the desired file already exists.
2) If so, then delete it before doing SaveAs.
This is not really possible as you don't have direct access to the files.

On my phone I can overwrite existing files. The exact behavior depends on the external app that handles the intent.
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Thanks. Because the user can choose the location, I need to know his choice.

I use:
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 ContentResolver As JavaObject = ctxt.InitializeContext.RunMethodJO("getContentResolver", Null)
        Dim out As OutputStream = ContentResolver.RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null), "wt")) 'wt = Write+Trim
        Log("Result:"&result.GetData)
        Log("Out:"&out)
        File.Copy2(Source, out)
        out.Close
        Return True
    End If
    Return False
End Sub

If I want to check if the file exist, how can I get the location?
Just before File.Copy2 I need to do something like this:
if (File.Exist(?????,Title)) then .........

How to get the ?????

Kind regards,
André
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Do not expect every media on your device is in a physical file.
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
I find a workaround to overwrite, but I don't know how to translate to B4X:
B4X:
Problem:
Storage Access Framework ACTION_CREATE_DOCUMENT has no option to overwrite an existing file.
A file such as "filename.ext (1)" will be created if "filename.ext" already exists in folder.

To minimize required work and to reuse existing code, the following method can be used:
1. Allow putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true) for ACTION_CREATE_DOCUMENT intent;
2. No change to picker UI (only a single filename as input in startActivityForResult);
3. If desired filename does not exist, onActivityResult returns Uri with getData();
4. If filename already exists, onActivityResult returns Uri with getClipData();
5. In the latter case, getClipData() has two items (new Uri and existing Uri).

Benefit:
1. Proposed method can reuse code for ACTION_OPEN_DOCUMENT (EXTRA_ALLOW_MULTIPLE tag);
2. Developer can offer (via dialog) to overwrite an existing file or create a new one;
3. ACTION_CREATE_DOCUMENT intent works the old way (without EXTRA_ALLOW_MULTIPLE tag).

Can someone help me to translate it?

Kind regards,
André
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. This is a feature request. The instructions you posted in the first post were never implemented.

1710396442603.png


2. The user can overwrite files with a long click on the existing file. It is a bit confusing but this is how Android works.
3. You cannot delete the file without the user first selecting it.
 
Upvote 0
Top