Show list of apps for file type and start the app

COBRASoft

Active Member
Licensed User
Longtime User
Hi,

I would like to show a list of installed apps on the device for 'any' given filetype. The user should be able to choose between those apps and my app should start that app and the file.

E.g. play a song or play a movie, show PDF or open a webbrowser or image viewer...

Anybody an idea how to achieve all this?

Greetings,
Sigurd
 

NJDude

Expert
Licensed User
Longtime User
One way of doing would be by getting the file extension and based on that trigger an intent, for example, let's say that the file I selected is an MP4 so I would do something like this:
B4X:
Dim I As Intent

I.Initialize(I.ACTION_VIEW, "")

I.SetType("video/*")

StartActivity(I)

If the file is Text then the SetType would be:

B4X:
I.SetType("text/*")

This is very crude, but maybe it will give you a clue.
 
Upvote 0

COBRASoft

Active Member
Licensed User
Longtime User
Not working like expected

Hi,

Thx for the tip, but it's only working for text. Other types like mp4, jpg, ... show a list of apps, but in the app the selected file doesn't open. m4a files gives an immediate error. What am I doing wrong here?

B4X:
Dim strTemp As String: strTemp = CurrentFolder.Replace("/mnt", "") & "/" & objView.Tag '--- Tag contains selected filename, placing "file//" in front doesn't help
Dim intent1 As Intent
intent1.Initialize(intent1.ACTION_VIEW, strTemp)
Select Case strExtension.ToLowerCase
    Case "txt": intent1.SetType("text/plain")
    Case "rtf": intent1.SetType("text/rtf")
    Case "csv": intent1.SetType("text/csv")
    Case "txt": intent1.SetType("text/plain")
    Case "png", "jpg", "jpeg", "bmp", "gif", "tif", "tiff": intent1.SetType("image/*")
    Case "xml", "fb2": intent1.SetType("text/*")
    Case "pdf": intent1.SetType("application/pdf")
    Case "mp4", "mpg", "avi", "mov", "mkv": intent1.SetType("video/*")
    Case "mp3", "m4a": intent1.SetType("audio/*")
    Case "zip": intent1.SetType("application/zip")
    Case Else: intent1.SetType("text/*")
End Select
Try
    StartActivity(intent1)
Catch
    ToastMessageShow("Cannot start file." & CRLF & strTemp, False)
End Try
Greetings,
Sigurd
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
This is a untested attempt using Reflection.


B4X:
Sub GetMimeType(ext as String) as String
Dim R as Reflector
Dim mime as Object
mime = R.CreateObject ("android.webkit.MimeTypeMap")

R.Target = mime

Dim mimetype as String
mimetype = R.RunMethod2("getMimeTypeFromExtension ", ext , "java.lang.String")
Return mimetype
End Sub
 
Upvote 0

COBRASoft

Active Member
Licensed User
Longtime User
The above code returns 'access to constructor not allowed'.

I think it has more to do with the uri too. Perhaps my filenames are not correct for pictures and some other extensions.
 
Upvote 0

COBRASoft

Active Member
Licensed User
Longtime User
Ok... Found that placing 'file://' in front of my filename solves a lot of problems. Now, everything seems to open for which I specified the mimetype. If the mimetype code above would work, it would work for all file types...
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
To further the discussion, I have created a small lib that encapsulates everything in MimeType.

Have a test of it.

The issue I face is that it narrows down the mimetype too much.
For e.g. an xml file will generate a mimetype of "text/xml" rather than "text/*" and if now application is registered to view xml files, it will fail with a toast message saying that it cannot open the filetype.

So the question is whether you want it narrow or not.
The solution would be to strip the mimetype and return "text/*"
 

Attachments

  • MimeType.zip
    1.6 KB · Views: 329
Upvote 0

andrewj

Active Member
Licensed User
Longtime User
Hi,
This is a very useful library, and I've managed to adapt your sample code for "start the appropriate external application" so it works well for known file types. Thanks very much for starting me off on this.

The challenge I have is that in many cases your library does not return a known Mime type even for file types which should be handle-able (e.g. .dng files don't show up as "images"). What's the suggested approach to such cases? Is there a standard tool in Android where you can manage the Mime type vs extension list? Alternatively should I try and provide a tool to manage this?
Thanks
Andrew
 
Upvote 0

andrewj

Active Member
Licensed User
Longtime User
Thanks. It looks like I need to build a little "extension manager" screen where the users can register unknown extensions as "image", "video" etc.
Otherwise this works very well.
Thanks
Andrew
 
Upvote 0
Top