OpenTracks: Export/Import file error for KMZ (with photos) files

Describe the bug

  1. When exporting KMZ (with photos) files from OpenTracks, some files generate parser errors. When attempting to import these same files back into the app, the errors persist. This issue appears to be version-specific, as it doesn’t occur in v4.17.8 and below
  2. Additionally, sharing these files to Organic Maps fails with a “failed to load bookmarks” error message. While Organic Maps doesn’t support photo markers (expected behavior), KMZ files with photos should still load with the track data visible but without markers. Instead, these files fail completely, suggesting a file format issue rather than just the photo marker limitation.

To Reproduce

  1. Export track data to KMZ (with photos) format
  2. Attempt to import the KMZ (with photos) files back into OpenTracks
  3. Alternatively, try sharing the exported KMZ (with photos) files to Organic Maps

Error Messages

  1. When importing back to OpenTracks:
<file-name>.kmz: Parser error:
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: no element found
  1. When sharing to Organic Maps:
"Failed to load bookmarks. The file may be corrupted or defective"
When share KMZ to Organic Maps from OpenTracks crash log ### App information * ID: de.dennisguse.opentracks.nightly * Version: 6121 v4.19.1

Device information

  • Brand: Redmi
  • Device: alioth
  • Model: M2012K11AC
  • Id: AQ3A.240912.001
  • Product: alioth

Firmware

  • SDK: 35
  • Release: 15
  • Incremental: OS2.0.5.0.VNKCNXM

Cause of error

Exception in thread "AsyncTask #1": java.lang.RuntimeException: An error occurred while executing doInBackground()
	at android.os.AsyncTask$4.done(AsyncTask.java:415)
	at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:381)
	at java.util.concurrent.FutureTask.setException(FutureTask.java:250)
	at java.util.concurrent.FutureTask.run(FutureTask.java:269)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1251)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:668)
	at java.lang.Thread.run(Thread.java:1012)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'float java.lang.Float.floatValue()' on a null object reference
	at de.dennisguse.opentracks.io.file.exporter.KMLTrackExporter.getHeading(KMLTrackExporter.java:478)
	at de.dennisguse.opentracks.io.file.exporter.KMLTrackExporter.writeMarker(KMLTrackExporter.java:277)
	at de.dennisguse.opentracks.io.file.exporter.KMLTrackExporter.writeMarkers(KMLTrackExporter.java:151)
	at de.dennisguse.opentracks.io.file.exporter.KMLTrackExporter.writeTrack(KMLTrackExporter.java:116)
	at de.dennisguse.opentracks.io.file.exporter.KMZTrackExporter.writeTrack(KMZTrackExporter.java:74)
	at de.dennisguse.opentracks.data.ShareContentProvider.lambda$openFile$3(ShareContentProvider.java:216)
	at de.dennisguse.opentracks.data.ShareContentProvider$$ExternalSyntheticLambda5.writeDataToPipe(D8$$SyntheticClass:0)
	at android.content.ContentProvider$1.doInBackground(ContentProvider.java:2572)
	at android.os.AsyncTask$3.call(AsyncTask.java:394)
	at java.util.concurrent.FutureTask.run(FutureTask.java:264)
	... 3 more

Screenshots

Technical information

  • Device: Poco F3
  • OS: Android 15 | HyperOS 2.0.5.0 (custom ROM)
  • OpenTracks version: v4.19.0 stable (playstore) or v4.19.1 Nightly (6121), all from fdroid
  • OpenTracks commit id (only for nightly builds): 6121?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 23 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Hello Dennis,

This is the problem and maybe even a solution. I mean the solution works, now whether it’s a good one, you should decide that. I’ll write the entire summary and fix, maybe it will help.

Problem Summary

When exporting tracks with photo markers in KMZ with photos format on some devices, the export fails and the resulting file is unusable. The logcat shows a crash:

Attempt to invoke virtual method 'float java.lang.Float.floatValue()' on a null object reference
at de.dennisguse.opentracks.io.file.exporter.KMLTrackExporter.getHeading(KMLTrackExporter.java:478)

Root Cause

  • The crash happens in the getHeading method of KMLTrackExporter.java.
  • The method tries to get the bearing (direction in degrees) for a marker’s position by calling position.bearing().
  • On some markers or devices, position.bearing() returns null (meaning the direction cannot be determined, e.g., due to missing or insufficient GPS data).
  • The original code returned this value directly as a float, which causes a NullPointerException when the value is null.

Why This Affects KMZ with Photos Export

  • The KMZ export process generates a KML file (using KMLTrackExporter) and then packages it with the photos.
  • When exporting photo markers, the code tries to write the camera heading in the KML.
  • If the bearing is null, the app crashes during KML generation, so the KMZ export fails.

What Was Changed and Why

File Modified:

src/main/java/de/dennisguse/opentracks/io/file/exporter/KMLTrackExporter.java

Method:

private float getHeading(Track.Id trackId, Position position)

Original Code:

private float getHeading(Track.Id trackId, Position position) {
    TrackPoint.Id trackPointId = contentProviderUtils.getTrackPointId(trackId, position);
    if (trackPointId == null) {
        return position.bearing();
    }
    TrackPoint viewLocation = contentProviderUtils.getLastValidTrackPoint(trackId);
    if (viewLocation != null) {
        Optional<Float> bearing = viewLocation.bearingTo(position);
        if (bearing.isPresent()) {
            return bearing.get();
        }
    }
    return position.bearing();
}

Problem:

  • If position.bearing() is null, this code tries to return null as a float, causing a crash.

Fixed Code:

private float getHeading(Track.Id trackId, Position position) {
    TrackPoint.Id trackPointId = contentProviderUtils.getTrackPointId(trackId, position);
    if (trackPointId == null) {
        Float b = position.bearing();
        return b != null ? b : 0f;
    }
    TrackPoint viewLocation = contentProviderUtils.getLastValidTrackPoint(trackId);
    if (viewLocation != null) {
        Optional<Float> bearing = viewLocation.bearingTo(position);
        if (bearing.isPresent()) {
            return bearing.get();
        }
    }
    Float b = position.bearing();
    return b != null ? b : 0f;
}

What the Fix Does:

  • Checks if position.bearing() is null.
  • If it is, returns 0f (float zero) as a safe default, which means “north” in navigation terms.
  • This prevents the crash and allows the export to complete, even if the bearing is missing.

Summary

  • The crash was due to a missing null check for bearing in KMLTrackExporter.
  • The fix is to safely handle null bearings by returning 0f.
  • This resolves the KMZ with photos export issue on all devices.

No problem. I’ll install it when it becomes available and post the logcat here again.

Thanks for investigating this issue.

This only affects KMZ files with photo markers - standard KML, GPX, and KMZ without photos export correctly.

Looking at the problematic KMZ exports, it seems the images directory is completely missing, containing only the doc.kml file inside the zip archive. In contrast, exports from v4.17.8 properly include the images directory for KMZ files with photo markers.