forked from upstream-mirrors/OpenTracks
KMZ exporting: checks waypoints images exists before parse kml.
This commit is contained in:
@@ -32,6 +32,7 @@ import de.dennisguse.opentracks.content.data.TrackPoint;
|
|||||||
import de.dennisguse.opentracks.content.data.Waypoint;
|
import de.dennisguse.opentracks.content.data.Waypoint;
|
||||||
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
||||||
import de.dennisguse.opentracks.content.sensor.SensorDataSet;
|
import de.dennisguse.opentracks.content.sensor.SensorDataSet;
|
||||||
|
import de.dennisguse.opentracks.util.FileUtils;
|
||||||
import de.dennisguse.opentracks.util.StringUtils;
|
import de.dennisguse.opentracks.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -162,7 +163,8 @@ public class KmlTrackWriter implements TrackWriter {
|
|||||||
@Override
|
@Override
|
||||||
public void writeWaypoint(Waypoint waypoint) {
|
public void writeWaypoint(Waypoint waypoint) {
|
||||||
if (printWriter != null && exportTrackDetail) {
|
if (printWriter != null && exportTrackDetail) {
|
||||||
if (waypoint.hasPhoto() && exportPhotos) {
|
boolean existsPhoto = FileUtils.getPhotoFileIfExists(context, waypoint.getTrackId(), waypoint.getPhotoURI()) != null;
|
||||||
|
if (waypoint.hasPhoto() && exportPhotos && existsPhoto) {
|
||||||
float heading = getHeading(waypoint.getTrackId(), waypoint.getLocation());
|
float heading = getHeading(waypoint.getTrackId(), waypoint.getLocation());
|
||||||
writePhotoOverlay(waypoint.getName(), waypoint.getCategory(), waypoint.getDescription(), waypoint.getLocation(), waypoint.getPhotoUrl(), heading);
|
writePhotoOverlay(waypoint.getName(), waypoint.getCategory(), waypoint.getDescription(), waypoint.getLocation(), waypoint.getPhotoUrl(), heading);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -112,7 +112,11 @@ public class KmzTrackExporter implements TrackExporter {
|
|||||||
}
|
}
|
||||||
Waypoint waypoint = contentProviderUtils.createWaypoint(cursor);
|
Waypoint waypoint = contentProviderUtils.createWaypoint(cursor);
|
||||||
if (waypoint.hasPhoto()) {
|
if (waypoint.hasPhoto()) {
|
||||||
addImage(context, zipOutputStream, waypoint.getPhotoUrl());
|
Uri uriPhoto = waypoint.getPhotoURI();
|
||||||
|
boolean existsPhoto = FileUtils.getPhotoFileIfExists(context, track.getId(), uriPhoto) != null;
|
||||||
|
if (existsPhoto) {
|
||||||
|
addImage(context, zipOutputStream, uriPhoto);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cursor.moveToNext();
|
cursor.moveToNext();
|
||||||
@@ -122,9 +126,7 @@ public class KmzTrackExporter implements TrackExporter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addImage(Context context, ZipOutputStream zipOutputStream, String photoUrl) throws IOException {
|
private void addImage(Context context, ZipOutputStream zipOutputStream, Uri uri) throws IOException {
|
||||||
Uri uri = Uri.parse(photoUrl);
|
|
||||||
|
|
||||||
try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) {
|
try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) {
|
||||||
ZipEntry zipEntry = new ZipEntry(buildKmzImageFilePath(uri));
|
ZipEntry zipEntry = new ZipEntry(buildKmzImageFilePath(uri));
|
||||||
zipOutputStream.putNextEntry(zipEntry);
|
zipOutputStream.putNextEntry(zipEntry);
|
||||||
|
|||||||
+2
-14
@@ -380,20 +380,8 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Uri externalPhotoUri = Uri.parse(externalPhotoUrl);
|
File file = FileUtils.getPhotoFileIfExists(context, importTrackId, Uri.parse(externalPhotoUrl));
|
||||||
if (externalPhotoUri == null) {
|
if (file != null) {
|
||||||
Log.w(TAG, "Could not parse external photo url.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String filename = externalPhotoUri.getLastPathSegment();
|
|
||||||
if (filename == null) {
|
|
||||||
Log.w(TAG, "External photo contains no filename.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
File dir = FileUtils.getPhotoDir(context, importTrackId);
|
|
||||||
File file = new File(dir, filename);
|
|
||||||
if (file.exists()) {
|
|
||||||
Uri photoUri = FileUtils.getUriForFile(context, file);
|
Uri photoUri = FileUtils.getUriForFile(context, file);
|
||||||
return "" + photoUri;
|
return "" + photoUri;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -250,4 +250,34 @@ public class FileUtils {
|
|||||||
public static Uri getUriForFile(Context context, File file) {
|
public static Uri getUriForFile(Context context, File file) {
|
||||||
return FileProvider.getUriForFile(context, FileUtils.FILEPROVIDER, file);
|
return FileProvider.getUriForFile(context, FileUtils.FILEPROVIDER, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks that there is a file inside track photo directory whose name is the same that uri file.
|
||||||
|
* If there is a file inside photo directory whose name is the same that uri then returns File. Otherwise returns null.
|
||||||
|
*
|
||||||
|
* @param context the Context.
|
||||||
|
* @param trackId the id of the Track.
|
||||||
|
* @param uri the uri to check.
|
||||||
|
* @return File object or null.
|
||||||
|
*/
|
||||||
|
public static File getPhotoFileIfExists(Context context, long trackId, Uri uri) {
|
||||||
|
if (uri == null) {
|
||||||
|
Log.w(TAG, "URI object is null.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String filename = uri.getLastPathSegment();
|
||||||
|
if (filename == null) {
|
||||||
|
Log.w(TAG, "External photo contains no filename.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
File dir = FileUtils.getPhotoDir(context, trackId);
|
||||||
|
File file = new File(dir, filename);
|
||||||
|
if (!file.exists()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user