B4J Question Save file to "documents" directory on Mac, Linux and Windows

Dominik H

Member
Licensed User
Longtime User
What can I do to save a file in directory depending on the user's OS? Can I do something that would detect the OS and then based on that save it in what ever the "Documents" folder is?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
Sub DirUserDefault As String
   Dim jo As JavaObject
   Return jo.InitializeStatic("javax.swing.filechooser.FileSystemView").RunMethodJO("getFileSystemView", _
     Null).RunMethodJO("getDefaultDirectory", Null).RunMethod("getAbsolutePath", Null)
End Sub

It will return the documents folder on Windows and the user home on Mac and Linux.
 
Upvote 0

Dominik H

Member
Licensed User
Longtime User
You can use this code:
B4X:
Sub DirUserDefault As String
   Dim jo As JavaObject
   Return jo.InitializeStatic("javax.swing.filechooser.FileSystemView").RunMethodJO("getFileSystemView", _
     Null).RunMethodJO("getDefaultDirectory", Null).RunMethod("getAbsolutePath", Null)
End Sub

It will return the documents folder on Windows and the user home on Mac and Linux.

Thank you very much! I would've never even thought of that.
 
Upvote 0

Dominik H

Member
Licensed User
Longtime User
You can use this code:
B4X:
Sub DirUserDefault As String
   Dim jo As JavaObject
   Return jo.InitializeStatic("javax.swing.filechooser.FileSystemView").RunMethodJO("getFileSystemView", _
     Null).RunMethodJO("getDefaultDirectory", Null).RunMethod("getAbsolutePath", Null)
End Sub

It will return the documents folder on Windows and the user home on Mac and Linux.

Yes this is old, but I've been busy so I've not had a chance to test it, but I did, and I got this error:
B4X:
Program started.
Error occurred on line: 36
java.lang.NumberFormatException: For input string: "(TextField) TextField@676d61ac[styleClass=text-input text-field], Text: export,"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at anywheresoftware.b4a.debug.RDebugUtils.numberCast(RDebugUtils.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:593)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:225)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.BA$2.run(BA.java:165)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

When running this piece of code:
B4X:
Dim out As OutputStream = File.OpenOutput(DirUserDefault, ExportText.toString + expType, False)
    Try
        Template.Snapshot.WriteToStream(out)
    Catch
        Log(LastException.Message)
    End Try

Template = An imageView
ExportText = TextField
expType = a string variable containing either ".jpg", ".png", ".bmp" (tested with jpg which is used by default)

What can I do about this?
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
you should use
B4X:
Dim out As OutputStream = File.OpenOutput(DirUserDefault, ExportText.text + expType, False)
, if you want to use the input of Exporttext as a part of the filename
 
Upvote 0

Dominik H

Member
Licensed User
Longtime User
you should use
B4X:
Dim out As OutputStream = File.OpenOutput(DirUserDefault, ExportText.text + expType, False)
, if you want to use the input of Exporttext as a part of the filename
I'm looking at this and thinking to myself why I didn't notice that myself, ahhh silly me. Thank you for pointing it out!
 
Upvote 0
Top