6

My Flutter (Android and iOS) app generates KML or GPX files, which works as expected.

Now I want to add a button "Open file" which should open such a system dialog where the installed map or other consuming apps (which can handle such formats) are listed to be chosen.

How can I open this dialog? How can I find the format-related apps and how can I send the file to these apps. I guess, there is a plugin for this, but couldn't find one. It must work on Android and iOS, nice-to have is web support.

I just saw open_file but I am not quite sure, if it works as expected because it doesn't list support for KML/GPX.

Thanks in advance!

S-Man
  • 22,521
  • 7
  • 40
  • 63
  • 2
    Did you try to launch an app using URI of the file? https://stackoverflow.com/questions/7965747/android-how-to-open-google-maps-with-intent-and-kml?lq=1 https://stackoverflow.com/questions/49345605/how-to-open-an-application-from-a-flutter-app – fdermishin Nov 26 '20 at 18:06
  • 1
    Nice idea, tried it, but `canLaunch(...)` is false unfortunately :( The error is `PlatformException(ACTIVITY_NOT_FOUND, No Activity found to handle intent {the\path\to\the\KML\file.kml}, null, null)` However, there are apps installed which can handle KML: Google Maps, Locus, ... and c:geo can handle GPX files – S-Man Nov 26 '20 at 21:15
  • When I add `file:///`, I get a `android.os.FileUriExposedException`. When searching for a solution, I find, that I have to add a `provider` to my AndroidManifest, but this doesn't help... – S-Man Nov 26 '20 at 21:45
  • Wouldn't it just be adding the MIME type of KML and GPX to the "open_file" plugin? – Morrison Chang Dec 01 '20 at 07:01
  • I am not quite sure how to do this. Especially I don't know any specific MIME type for GPX. – S-Man Dec 01 '20 at 07:02
  • See: https://en.wikipedia.org/wiki/GPS_Exchange_Format and as the [open_file Flutter plugin is open source](https://github.com/crazecoder/open_file) you could fork it and add it in (just look for the other mime types in the files) or put in a feature request. – Morrison Chang Dec 01 '20 at 07:21
  • @MorrisonChang Would you mind to write this into an answer to earn the bounty? – S-Man Dec 03 '20 at 08:56

1 Answers1

1

If you found a Flutter plug-in that is open source like open_file, you could just add in the MIME types yourself.

For KML format: https://en.wikipedia.org/wiki/Keyhole_Markup_Language

For GPX format: https://en.wikipedia.org/wiki/GPS_Exchange_Format

As the package is open source I was thinking of modifying the code, as the mapping appears to be straight forward:

https://github.com/crazecoder/open_file/blob/master/ios/Classes/OpenFilePlugin.m

and

https://github.com/crazecoder/open_file/blob/master/android/src/main/java/com/crazecoder/openfile/OpenFilePlugin.java

You would need to fork the projects and add in the types you wanted.

Alternately you've posted issue ticket #116

where the repo owner responded with

OpenFile.open("/sdcard/example.txt", type: "text/plain", uti: "public.plain-text");

Do respond if that works, as I was uncertain if both of the different platforms' filetype (uti vs MIME type) works universally in Flutter or if you have platform specific detection code to assign the filetype correctly.

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77