Android Question get file extension

mehdimf20

Member
Licensed User
Hi All,
How to get file extension in ContentChooser ?

B4X:
Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
 
    If Success Then
         Dim m As MLfiles
         Log(m.GetFileType(FileName))   ' return unknown
    End If
End Sub
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code.

It is not possible in all cases to get the Extension.

What do you need it for? You can open the file using the given path and filename anyway.
 
Upvote 0

mehdimf20

Member
Licensed User
Please use [CODE]code here...[/CODE] tags when posting code.

It is not possible in all cases to get the Extension.

What do you need it for? You can open the file using the given path and filename anyway.

use this code for get image or video and show image or video.
B4X:
chooser.Show("video/mp4,image/*", "select")
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code.

It is not possible in all cases to get the Extension.

What do you need it for? You can open the file using the given path and filename anyway.

Hi @DonManfred . I have the same problem... answering your question (what do you need it for?), should be:

1. Rotate a JPEG image if needed before loading into imageview...
2. Show a GIF in the correct object, and be able to copy to a .gif extension in internal dir, maintaining the image integrity...
3. Play a video when receiving a mpeg extension...

I could list many other causes to know the extension of a file chosen by a content chooser before processing it, but those are my reasons. and so: how can we do that?

I think that I could load the file in a byte stream and parse it to find the content types signatures, but for me really looks complicated... must to exist other way... can U help us?
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
I could list many other causes to know the extension of a file chosen by a content chooser before processing it, but those are my reasons. and so: how can we do that?
I think that I could load the file in a byte stream and parse it to find the content types signatures, but for me really looks complicated... must to exist other way... can U help us?

The following is a brute force routine I have used in the past to get the file extension:
(Note: this is written off the top-of-my-head. There might be some typo's)

B4X:
Sub GetFileExt(FileName As String) as String
  Return(FileName.SubString2(FileName.LastIndexof("."), FileName.Length)
End Sub

Barry.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
The following is a brute force routine I have used in the past to get the file extension:
(Note: this is written off the top-of-my-head. There might be some typo's)

B4X:
Sub GetFileExt(FileName As String) as String
  Return(FileName.SubString2(FileName.LastIndexof("."), FileName.Length)
End Sub

Barry.
Hi,

this routine will not work. When came from gallery thru content chooser, filename is an url relative to gallery app and will not have a name with extension. Its something like this:

content://media/external/images/media/16

The solution is to get the file, write as bytes with a generic name, parse it and identify the signature of each type (jpeg,mpeg,gif) ... I think that if the content chooser has image/* parameter, it's possible to get gif, jpg or png for example. Like @Erel already told in a post, there is no problem if you copy a png image as jpg BUT if it's a gif? You will loose data of course. And more: jpg image has rotation parameter and sometimes it's needed to rotate before loading on the screen. Then, IF your content chooser is image/* it's really needed to know the extension of source file to proceed the correct image processing.

For now, my solution is to separate: image/jpg and image/png from image/gif , with different content choosers/routines in the activity.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
When came from gallery thru content chooser, filename is an url relative to gallery app and will not have a name with extension. Its something like this:

content://media/external/images/media/16

Thanks. Did not realize that the filename had a relative format.
This info will come in handy when I need to use the chooser.

Barry.
 
Upvote 0
Top