I'm trying to understand how to save using Content Chooser, so the user can give a name to the file
I have been trying my best to find something similiar on the forum to guide me, and I found a Loader using the same technique.
How could I trig a Save requester and get the filename and path from the user?
B4X:
Definitions
Dim Chooser As ContentChooser
' Load Trigger
Chooser.Initialize("chooser")
Chooser.Show("image/*", "Select an image")
' Load Callback
Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then
Log("Loading")
Log(Dir)
Log(FileName)
Else
ToastMessageShow("No image selected", True)
End If
End Sub
Dim CC As ContentChooser : CC.Initialize("CC")
CC.Show("image/*", "Choose image")
Wait For (CC) CC_Result (Success As Boolean, Dir As String, FileName As String)
If Success = True Then
Wait For (File.CopyAsync(Dir,FileName, File.DirInternalCache, "tmp_img.jpg")) Complete (Success As Boolean)
If Success Then
'File.Combine(File.DirInternalCache, "tmp_img.jpg") 'you can now get your image
End If
Else
ToastMessageShow("No Success :(",True)
End If
Thanks, I looked at his list a few times and this is what I'm doing now, I picked ContentChooser because I need 4.4 support as well as API 30+ .
The loading is working , but I am not understanding saving yet. My goal later on will be to support SAF but i'm really not sure I can handle XUI to load and save images easily. So I'm startng with Contentchose.
Lots of ways seem to varnish but I am using the Pictures folder, which seems to be available still.
"ContentChooser (Phone libray) - allows the user to select a resource or file using external providers. Very useful and doesn't require permissions.
ExternalStorage - Allows accessing external (secondary) storages. Requires the user to first select the target folder. Once the user selected a folder, your app can read and write from that folder. Doesn't require any permission.
Not all folders are accessible with ExternalStorage in Android 11+. Specifically, root, Android/data and Download are not accessible: more information.
SaveAs- this code, the opposite of ContentChooser or the simpler version of ExternalStorage. Allows the user to choose the place where the file will be saved. Simple to work with and doesn't require permissions. Possible alternative to the external storage permission, which is mostly no longer available.
RuntimePermissions.GetSafeDirDefaultExternal - A folder on the secondary storage, where you app can access without permissions. The path is a bit cumbersome (Log it to see)."
Dim CC As ContentChooser : CC.Initialize("CC")
CC.Show("image/*", "Choose image")
Wait For (CC) CC_Result (Success As Boolean, Dir As String, FileName As String)
If Success = True Then
Wait For (File.CopyAsync(Dir,FileName, File.DirInternalCache, "tmp_img.jpg")) Complete (Success As Boolean)
If Success Then
'File.Combine(File.DirInternalCache, "tmp_img.jpg") 'you can now get your image
End If
Else
ToastMessageShow("No Success :(",True)
End If
I'm not using any cache as your code., I think I need to experiment abit more so I can describe the problem.
It's basically the question "how do you save an image". but I see this is overly complex I realise.
The load works but the save goes to the root instad of pictures folder and i don't get to pick the name and this is tricky to get around at first.
Dim x As B4XBitmap = YourBitmap
Dim out As OutputStream = File.OpenOutput(xui.DefaultFolder, "1.png", False)
x.WriteToStream(out, 100, "PNG")
out.Close
It is, and in a way that is works from Android 4.4 an up. I will find a way to convert between what i use (pixmap) and bitmap and but 'm trying to find a the best wat to allow the user to save a png by letting them pick a path and name for the picture and click save.
I'm thinking, as I am using images I'm covered by mediastore which is supposed to be a goo abalance of simple code and modern mechanism to get file (well images etc). SAF is only neededd for other files in modern API's so I can skip that.
This would mean one intent for load and one for save:
ACTION_OPEN_DOCUMENT (for load image)
ACTION_CREATE_DOCUMENT (for save image)