B4J Code Snippet Find user documents folder

Status
Not open for further replies.
B4X:
Sub FindUserDocumentsFolder As String
   If DetectOS = "windows" Then
       Dim jo As JavaObject
       Dim fs As JavaObject = jo.InitializeStatic("javax.swing.filechooser.FileSystemView").RunMethod("getFileSystemView", Null)
       Return fs.RunMethodJO("getDefaultDirectory", Null).RunMethod("getPath", Null)
   Else
       Return GetSystemProperty("user.home", "") & "/Documents"
   End If
End Sub

Sub DetectOS As String
   Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
   If os.Contains("win") Then
       Return "windows"
   Else If os.Contains("mac") Then
       Return "mac"
   Else
       Return "linux"
   End If
End Sub

Tested on Windows and Mac only.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
What is the prefferable way to open the path in explorer/finder

ShowExternalDocument or Shell?
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private shl As Shell
End Sub

Sub shl.Initialize("Shell","cmd",Array As String("/c", "start", "E:/Basic4android/Projekte/libs.DonManfred/"))
    shl.Run(5000)
End Sub
Sub Shell_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log($"Shell_ProcessCompleted(${Success}, ${ExitCode}, ${StdOut}, ${StdErr})"$)
End Sub
It opens this Folder in my Explorer. Just tested on Windows



Using ShowExternalDocument it works this way

B4X:
fx.ShowExternalDocument(File.GetUri("E:\Basic4android\Projekte\libs.DonManfred", "CalendarView"))

It opens E:\Basic4android\Projekte\libs.DonManfred\CalendarView\


It does work this way too
B4X:
fx.ShowExternalDocument(File.GetUri("E:\Basic4android\Projekte\libs.DonManfred", ""))
It opens E:\Basic4android\Projekte\libs.DonManfred

Based on these two examples i would prefer the ShowExternalDocument Solution as you do not need to start a Shell and a cmd instance.

PS: I cannot say anywhing about Mac; you need to test it.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
If you want to Open the Explorer and select a file/Folder

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    'Private shl As Shell
    Private NativeMe As JavaObject

End Sub
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    NativeMe = Me
    MainForm = Form1
    MainForm.Show
    NativeMe.RunMethod("OpenExplorer", Array As String(File.Combine("E:\Basic4android\Projekte\libs.DonManfred","CalendarView")))
End Sub
#If java
import java.lang.Runtime;
public static void OpenExplorer(String path){
   try {
       Runtime.getRuntime().exec("explorer.exe /select," + path);
   } catch (Exception e) {
   
}
}
#End If
It opens the Folder E:\Basic4android\Projekte\libs.DonManfred and select the Folder CalendarView

 

ThRuST

Well-Known Member
Licensed User
Longtime User
@DonManfred Very nice. This is most useful, I will try this on my virtual Mac which is running OSX El Capitan.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…