Android Question how to get access to DirDefaultExternal

Markos

Active Member
Licensed User
Longtime User
Hi All,

I noticed three(3) methods to get DirDefaultExternal path/string are all of these methods equivalent in Android 33+?


B4X:
dim path1 as string

Dim ctxt As JavaObject
    ctxt.InitializeContext

path1 = ctxt.RunMethod("getExternalFilesDir", Array("")))

path1 = File.DirDefaultExternal

path1 = rp.GetSafeDefaultExternal
 

Markos

Active Member
Licensed User
Longtime User
I will answer again for the 3rd time. The options available are listed here: SaveAs - Let the user select a target folder + list of other related methods

The only relevant method is rp.GetSafeDirDefaultExternal. There are very few cases where it is actually useful.
Hi Erel,

Sorry for testing your patience, and thanks for your patience. I think I now know my confusion, Does SaveAs give me the capability to empower the user to save any file type to any location on the device except .DirRootExternal or includes .DirRootExternal. If does not include .DirRootExternal which path is available so the user can save to a location that can actually be shared or accessed by another application?

And lastly if I have a pdf file or jpg or any other file type to save is the below code applicable and if not what do I have to change:

B4X:
Private Sub Button1_Click
'assume sa is a byte array populated globally

    File.WriteBytes(File.DirInternal, "test.jpg", sa) 'just for the example.
    Wait For (SaveAs(File.OpenInput(File.DirInternal, "test.jpg"), "application/octet-stream", "test.txt")) Complete (Success As Boolean)
    Log("File saved successfully? " & Success)
End Sub
 
Upvote 0

Markos

Active Member
Licensed User
Longtime User
I tried the SaveAs and it gave the error while I expected a dialogue popup to selected where to save the file, shown below
intenterror.jpg
 
Upvote 0

zed

Active Member
Licensed User
What's the point of answering you since you don't take the answers into account?
Points 8-9-10


this is the solution
 
Upvote 0

Markos

Active Member
Licensed User
Longtime User
What's the point of answering you since you don't take the answers into account?
Points 8-9-10


this is the solution
Thanks for replying.

I did try the SaveAs and I got an error, saying it's the solution is fine but it has to be tweaked if it all possible for my purpose. if you noticed the example was for trivial Text file and some parts might have to be changed. have you used this method successfully then plz offer some advice if a dialog should pop up from the user perspective OR it must be included in code, which it cannot be as the code doe snot explicitly proactively provide the destination path so it must have a dialogue popup , the intent is failing for some obvious reason which escapes me from the example as some pretext or manifest setting must be missing
 
Upvote 0

Markos

Active Member
Licensed User
Longtime User
I will answer again for the 3rd time. The options available are listed here: SaveAs - Let the user select a target folder + list of other related methods

The only relevant method is rp.GetSafeDirDefaultExternal. There are very few cases where it is actually useful.
I noticed a possible typo as ion was not declared in the below code, should (i as Intent) be (ion as Intent)

B4X:
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

or should it be as below

B4X:
Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    Dim ion As Object
    
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array(ion, i))
End Sub
 
Upvote 0

zed

Active Member
Licensed User
Here is the procedure I use to save a TXT file
B4A:
Private Sub exportTxt(text As String)
    'write
    File.WriteString(xui.DefaultFolder, "exp1.txt", text)
    'Save as
    Dim in As InputStream = File.OpenInput(xui.DefaultFolder, "exp1.txt")
    Wait For (FileHandler1.SaveAs(in, "text/plain", "YourFile.txt")) Complete (Success As Boolean)
    If Success Then
        toast.Show("File saved successfully")
    Else
        toast.Show("File not saved")
    End If
End Sub

In your code, you save a JPEG as TXT with OCTET/STREAM. MIME must match file type
Ex.
"text/plain", "YourFile.txt"
"application/pdf", "YourFile.pdf"
"image/png", "YourFile.png"
"image/jpeg", "YourFile.jpg"
 
Upvote 0

Markos

Active Member
Licensed User
Longtime User
Ok the dialogue to select path is coming up when I declare ion as Object. It now saves the file with no content anywhere I choose but app crashes after. I will explore further before asking for advice unless of course an immediate thought on what I need to do to avoid the crash and file with 0bytes being written.
 
Upvote 0

Markos

Active Member
Licensed User
Longtime User
Here is the procedure I use to save a TXT file
B4A:
Private Sub exportTxt(text As String)
    'write
    File.WriteString(xui.DefaultFolder, "exp1.txt", text)
    'Save as
    Dim in As InputStream = File.OpenInput(xui.DefaultFolder, "exp1.txt")
    Wait For (FileHandler1.SaveAs(in, "text/plain", "YourFile.txt")) Complete (Success As Boolean)
    If Success Then
        toast.Show("File saved successfully")
    Else
        toast.Show("File not saved")
    End If
End Sub

In your code, you save a JPEG as TXT with OCTET/STREAM. MIME must match file type
Ex.
"text/plain", "YourFile.txt"
"application/pdf", "YourFile.pdf"
"image/png", "YourFile.png"
"image/jpeg", "YourFile.jpg"
many thanks for that, I was afraid of having to explicitly state the mime, I wonder if we could use */* as I don't want to limit the mime's to what is currently known and also omit custom mime aliases.

I am using SaveAs without filehandler class maybe I'm missing some code to make it work perfectly. I will try initially to state explicitly the mime image/* and see if that works before going into code analysis mode.

Cheers for that
 
Upvote 0

zed

Active Member
Licensed User
I don't know the data that composes your file. It's text, images, byte. It would be easier to have an example to test.
 
Upvote 0

Markos

Active Member
Licensed User
Longtime User
Ok the dialogue to select path is coming up when I declare ion as Object. It now saves the file with no content anywhere I choose but app crashes after. I will explore further before asking for advice unless of course an immediate thought on what I need to do to avoid the crash and file with 0bytes being written.
Ok got it working using application/octet-stream for all files so dont have to state mime explicitly so far.. Found reason for the crash I closed the inputstream before the outputstream closed, my issue is because reading a 256kb file is a biit longer than a small text file I'm thinking.
 
  • Like
Reactions: zed
Upvote 0
Top