diff --git a/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmlTrackWriter.java b/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmlTrackWriter.java index 79b19c752..393ab19ca 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmlTrackWriter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmlTrackWriter.java @@ -74,7 +74,7 @@ public class KmlTrackWriter implements TrackWriter { /** * @param context the context * @param hasMultipleTracks should encode multiple tracks into one file? - * @param exportTrackDetail should detailed information about the track be exported (e.g., title, description, waypoints, timing)? + * @param exportTrackDetail should detailed information about the track be exported (e.g., title, description, waypoints, timing)? * @param exportSensorData should {@link SensorDataSet} be exported? * @param exportPhotos should pictures be exported (if true: exports to KMZ)? */ @@ -192,7 +192,8 @@ public class KmlTrackWriter implements TrackWriter { this.startTrackPoint = startTrackPoint; if (printWriter != null) { String name = context.getString(R.string.marker_label_start, track.getName()); - writePlacemark(name, "", "", START_STYLE, startTrackPoint.getLocation()); + Location location = startTrackPoint != null ? startTrackPoint.getLocation() : null; + writePlacemark(name, "", "", START_STYLE, location); printWriter.println(""); if (exportTrackDetail) { @@ -218,7 +219,8 @@ public class KmlTrackWriter implements TrackWriter { if (exportTrackDetail) { String name = context.getString(R.string.marker_label_end, track.getName()); String description = descriptionGenerator.generateTrackDescription(track, false); - writePlacemark(name, "", description, END_STYLE, endTrackPoint.getLocation()); + Location location = endTrackPoint != null ? endTrackPoint.getLocation() : null; + writePlacemark(name, "", description, END_STYLE, location); } } } @@ -345,7 +347,7 @@ public class KmlTrackWriter implements TrackWriter { writeCategory(category); if (exportPhotos) { - printWriter.println("" + Uri.decode(photoUrl) + ""); + printWriter.println("" + KmzTrackExporter.buildKmzImageFilePath(Uri.parse(photoUrl)) + ""); } printWriter.print(""); diff --git a/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmzTrackExporter.java b/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmzTrackExporter.java index e55a6410a..ef24556dc 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmzTrackExporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmzTrackExporter.java @@ -126,7 +126,7 @@ public class KmzTrackExporter implements TrackExporter { Uri uri = Uri.parse(photoUrl); try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) { - ZipEntry zipEntry = new ZipEntry(KMZ_IMAGES_DIR + File.separatorChar + FileUtils.sanitizeFileName(uri.getLastPathSegment())); + ZipEntry zipEntry = new ZipEntry(buildKmzImageFilePath(uri)); zipOutputStream.putNextEntry(zipEntry); if (inputStream == null) throw new FileNotFoundException(); @@ -147,4 +147,13 @@ public class KmzTrackExporter implements TrackExporter { outputStream.write(buffer, 0, byteCount); } } + + /** + * Builds and returns the path for the file that will be saved inside KMZ_IMAGES_DIR. + * + * @param uri URI object. + */ + public static String buildKmzImageFilePath(Uri uri) { + return KMZ_IMAGES_DIR + File.separatorChar + FileUtils.sanitizeFileName(uri.getLastPathSegment()); + } } \ No newline at end of file diff --git a/src/main/java/de/dennisguse/opentracks/io/file/importer/AbstractFileTrackImporter.java b/src/main/java/de/dennisguse/opentracks/io/file/importer/AbstractFileTrackImporter.java index 303b196a9..91cb59b0f 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/importer/AbstractFileTrackImporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/importer/AbstractFileTrackImporter.java @@ -372,9 +372,13 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track if (importTrackId == -1L) { return null; } + File dir = FileUtils.getPhotoDir(context, importTrackId); File file = new File(dir, fileName); - return Uri.fromFile(file).toString(); + + Uri photoUri = FileUtils.getUriForFile(context, file); + + return photoUri.toString(); } /** diff --git a/src/main/java/de/dennisguse/opentracks/io/file/importer/KmlFileTrackImporter.java b/src/main/java/de/dennisguse/opentracks/io/file/importer/KmlFileTrackImporter.java index 5721b6011..f03b7fe97 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/importer/KmlFileTrackImporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/importer/KmlFileTrackImporter.java @@ -186,9 +186,9 @@ public class KmlFileTrackImporter extends AbstractFileTrackImporter { return; } + // If there is photoUrl it has to be changed because that url in kml file isn't a valid content:// to the imported one. if (photoUrl != null) { - Uri uri = Uri.parse(photoUrl); - photoUrl = getPhotoUrl(uri.getLastPathSegment()); + photoUrl = getPhotoUrl(Uri.parse(photoUrl).getLastPathSegment()); } addWaypoint();