How to import, create and use gpx and kml track files using App Inventor Map Part II Use SAF extension to select kml/gpx files stored in your Documents folder

How to import, create and use gpx and kml track files using App Inventor Map Part II .. Use SAF extension (Storage Access Framework) to select kml/gpx files stored in your Documents folder.

icon

This advanced tutorial:

  • builds an app that displays kml and gpx tracking files like these that you might download :

  • shows how to place kml and gpx tracking files into the Android Documents folder

  • shows how to place the tracking files you load into Documents into an app's ASD. Once in the ASD the tracking file coordinates can be transferred to an App Inventor LineString and displayed as a route on a Map component.

  • shows the code to build the KML/GPX Viewer app.

  • the app example displays the kml/gpx files that are in the app's Private storage (ASD) in a ListPicker.

  • :railway_track: The KML/GPX File Viewer example uses the DjGPXTools extension 1/1/2021 version 2; and SAF extension Extension Version: 1.2 Date Built: 2023-07-03

DO NOT run the aia from the Companion aia, instead BUILD the apk using Build>Android App (.apk)

Part I of the tutorial is 🛤️ How to import, create and use gpx and kml track files to App Inventor Map

================


First things

  • Use a File manager and a USB connection to move several of your kml and/or gpx files into your Android Documents folder to test. The app will read these files; copy them to the app APD and display in an App Inventor LineString and Map.

  • Not all kml/gpx files can be successfully copied. A complex track file may not generate a LineString rendition of the track coordinates when using the DmGPXTools extension.

  • An example of a kml that works is Daudenberg-Route.kml. The file is attached in the aia Media in the attached aia. Left click the kml file that is in Media and select Download to my computer. After downloading to your PC, copy the example file to Documents as indicated above.

  • load the KML/GPX Viewer app aia file in App Inventor then BUILD the Project. Build>
    Android App (.apk)

  • after the app is built, load the app into your Android. You can load your kml and/or gpx track files using the Import Tools.


How to make the app smart/ add files to the ASD

  • When you first use the app after compiling the aia, the app is ignorant. The kml or gpx files in Documents need to be loaded / copied into the app's ASD using the SAF extension. Copying is done one file at a time.

Use the
importTools1 button to display the app's configuration and loading screen

  1. use the Select kml/gpx from Documents button to select a file from the Documents folder. You should see only the kml and gpx files stored in Documents (a filter is applied to the Document folder file extensions).

  2. use the Save SAF to ASD button to copy the file to the ASD. This step is needed to copy the track file to the ASD and the track file name appears in the **** ListPicker.

  3. use the Import Tools button to hide the tools.


DELETE a file from the ASD

  • to delete a file from the ASD from your app's Private storage:

deletefromASD2

  1. use the list view to select a kml file stored in the app's ASD you want to DELETE
  2. press the Remove from ASD button
  3. close the tool by clicking on Import Tools button. Alternatively, type the name of the file to be deleted into the the TextBox (that indicates enter kml File and then the Remove from ASD button. The code is:

  • to ensure the file is actually deleted, use the select2 button to view all the files still in the ASD. View the files and confirm the track file is no longer resident in the ASD.

How to view the files stored in the app

  • use the Select KML/GPX and select a track file.

  • to review another track file, select again.


What you got

  • the app displays the kml/gph files that are stored in the app's Private storage (ASD). The app is basically a template demonstrating one way to use the SAF extension to read kml and gpx files from the Documents folder, regardless of the tracking file's source.

  • In order to post your present location on the Map using the app you need to do some coding. I do not include that feature. Use a LocationSensor as shown in Part I to the tutorial. How to create your own tracks is shown in Part I. You probably will either write the code to get those files using the example described in Part I (CreateGPX or CreateKML) into the ASD or set up a separate system to save and view those tracks.



Key parts of the viewer app:

mimetypes

The app needs to be told which files it can use in the Documents folder. To be able to use kml and gpx files, the mime types SAF requires are:

  • application/vnd.google-earth.kml+xml for kml

  • application/octet-stream for gpx

  • Extensions

  1. SAF: App Inventor implementation of Storage Access Framework

  2. [FREE]DjGPXTool extension - Extensions - Kodular Community


Testing

The app example was tested on a Samsung A13 Android 13 cell phone.


Thank you

Anke and Sunny regarding SAF extension advice; Tim with respect to the ASD and others in the community. Django and Sunny for their extensions .


The aia. DO NOT run the aia from the Companion aia, instead BUILD the apk using Build>Android App (.apk)

Once you have built and loaded the KML_GPX_Viewer app, follow the directions above to populate the app's ASD with track files. :wink: and start viewing your tracking files.

KML_GPX_Viewer.aia (59.8 KB)


Regards,
SteveJG

7 Likes