B4J Question Retrieving file types?

techknight

Well-Known Member
Licensed User
Longtime User
How would I go about getting the file types of each file detected by File.ListFiles?

I know there is a test to see if a file is a "Directory" or not, but I was wondering if there is an easy way to distinguish "Video" files from "Image" files, or "Text" files, etc.

I cant always rely on the file extension, so I am curios.

any ideas?
 

Daestrum

Expert
Licensed User
Longtime User
You can use a combination of NIO and Google Guava library to determine filetypes.
NIO will (if it recognises the file) return a mime type string.
Using this string in Guava, you can test if it is ANY_VIDEO_TYPE or ANY_IMAGE_TYPE or ANY_TEXT_TYPE.

Example code (requires JavaObject library and Guava.jar) (note** I have older version of guava ver 21 you should use ver 23)

B4X:
#Region Project Attributes 
 #MainFormWidth: 600
 #MainFormHeight: 600
 #AdditionalJar: c:/temp/guava-21.0.jar  
#End Region
Sub Process_Globals
 Private fx As JFX
 Private MainForm As Form
 Dim MediaType As JavaObject
End Sub
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 MainForm.Show
 MediaType.InitializeStatic("com.google.common.net.MediaType")
 ' see https://google.github.io/guava/releases/23.0/api/docs/com/google/common/net/MediaType.html for types
 Dim fileType As String = asJO(Me).RunMethod("getType",Array("c:/temp/hannah.jpg")) ' the file to examine
 'what nio sees it as
 Log(fileType)
 ' use guava to group them into types of file video, image, text etc
 If fileType<>"null" Then
  If MediaType.RunMethodJO("parse",Array(fileType)).RunMethod("is",Array(MediaType.GetField("ANY_IMAGE_TYPE"))) Then
   Log("Image file")
  else if MediaType.RunMethodJO("parse",Array(fileType)).RunMethod("is",Array(MediaType.GetField("ANY_VIDEO_TYPE"))) Then
   Log("Video file")
  else if MediaType.RunMethodJO("parse",Array(fileType)).RunMethod("is",Array(MediaType.GetField("ANY_TEXT_TYPE"))) Then
   Log("Text file")
  Else
   Log("File is  : "&MediaType.RunMethod("parse",Array(fileType)))
  End If
 End If
End Sub
Sub asJO(o As JavaObject)As JavaObject
 Return o
End Sub
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub
#if java
import java.nio.file.*;
import java.io.*;
public static String getType(String filePath) throws IOException , SecurityException{
 return Files.probeContentType(Paths.get(filePath));
}
#End If
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
You can use a combination of NIO and Google Guava library to determine filetypes.
NIO will (if it recognises the file) return a mime type string.
Using this string in Guava, you can test if it is ANY_VIDEO_TYPE or ANY_IMAGE_TYPE or ANY_TEXT_TYPE.

Example code (requires JavaObject library and Guava.jar) (note** I have older version of guava ver 21 you should use ver 23)

B4X:
#Region Project Attributes
 #MainFormWidth: 600
 #MainFormHeight: 600
 #AdditionalJar: c:/temp/guava-21.0.jar 
#End Region
Sub Process_Globals
 Private fx As JFX
 Private MainForm As Form
 Dim MediaType As JavaObject
End Sub
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 MainForm.Show
 MediaType.InitializeStatic("com.google.common.net.MediaType")
 ' see https://google.github.io/guava/releases/23.0/api/docs/com/google/common/net/MediaType.html for types
 Dim fileType As String = asJO(Me).RunMethod("getType",Array("c:/temp/hannah.jpg")) ' the file to examine
 'what nio sees it as
 Log(fileType)
 ' use guava to group them into types of file video, image, text etc
 If fileType<>"null" Then
  If MediaType.RunMethodJO("parse",Array(fileType)).RunMethod("is",Array(MediaType.GetField("ANY_IMAGE_TYPE"))) Then
   Log("Image file")
  else if MediaType.RunMethodJO("parse",Array(fileType)).RunMethod("is",Array(MediaType.GetField("ANY_VIDEO_TYPE"))) Then
   Log("Video file")
  else if MediaType.RunMethodJO("parse",Array(fileType)).RunMethod("is",Array(MediaType.GetField("ANY_TEXT_TYPE"))) Then
   Log("Text file")
  Else
   Log("File is  : "&MediaType.RunMethod("parse",Array(fileType)))
  End If
 End If
End Sub
Sub asJO(o As JavaObject)As JavaObject
 Return o
End Sub
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub
#if java
import java.nio.file.*;
import java.io.*;
public static String getType(String filePath) throws IOException , SecurityException{
 return Files.probeContentType(Paths.get(filePath));
}
#End If

Not sure where to find this guava thing. Also, This doesnt require the internet does it? Because if it dose, that's out too because I cannot guarantee the system has access to the internet 100% of the time.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Your hourglass is broken. No idea what that means. Didn't even think anyone uses those anymore.

What I am doing is pretty straight forward. Taking the example above and attempting to use it. At least I thought it was straight forward. But, I have all my file handling subroutines in its own class that I called "FileManager"

So I am trying to add this code in my class, but the class doesn't like the AdditionalJar declaration.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
the AdditionalJar declaration goes in the Main module,
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I think he meant - "My Crystal Ball is broken" - probably implying - he can't read your mind...
Thank you! Yes, that´s what i meant to say.
Sorry, english is not my native language. So, "Crystal Ball" (instead of Hourglas) is the word i should use in future ;-)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
So I am trying to add this code in my class, but the class doesn't like the AdditionalJar declaration.
as written by @stevel05 the #additionaljar line must be inside the main.

Sidenote: If a class depends on a specific additional jar you should add a description for the dependencies inside the Setup-Documentation.
 
Upvote 0
Top