KMZ exporting: checks waypoints images exists before parse kml.

This commit is contained in:
Román
2020-04-14 14:58:52 +02:00
parent 0f1d8500d1
commit fa9cb3903c
4 changed files with 41 additions and 19 deletions
@@ -32,6 +32,7 @@ import de.dennisguse.opentracks.content.data.TrackPoint;
import de.dennisguse.opentracks.content.data.Waypoint;
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
import de.dennisguse.opentracks.content.sensor.SensorDataSet;
import de.dennisguse.opentracks.util.FileUtils;
import de.dennisguse.opentracks.util.StringUtils;
/**
@@ -162,7 +163,8 @@ public class KmlTrackWriter implements TrackWriter {
@Override
public void writeWaypoint(Waypoint waypoint) {
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());
writePhotoOverlay(waypoint.getName(), waypoint.getCategory(), waypoint.getDescription(), waypoint.getLocation(), waypoint.getPhotoUrl(), heading);
} else {
@@ -112,7 +112,11 @@ public class KmzTrackExporter implements TrackExporter {
}
Waypoint waypoint = contentProviderUtils.createWaypoint(cursor);
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();
@@ -122,9 +126,7 @@ public class KmzTrackExporter implements TrackExporter {
}
}
private void addImage(Context context, ZipOutputStream zipOutputStream, String photoUrl) throws IOException {
Uri uri = Uri.parse(photoUrl);
private void addImage(Context context, ZipOutputStream zipOutputStream, Uri uri) throws IOException {
try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) {
ZipEntry zipEntry = new ZipEntry(buildKmzImageFilePath(uri));
zipOutputStream.putNextEntry(zipEntry);
@@ -380,20 +380,8 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
return null;
}
Uri externalPhotoUri = Uri.parse(externalPhotoUrl);
if (externalPhotoUri == 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()) {
File file = FileUtils.getPhotoFileIfExists(context, importTrackId, Uri.parse(externalPhotoUrl));
if (file != null) {
Uri photoUri = FileUtils.getUriForFile(context, file);
return "" + photoUri;
}
@@ -250,4 +250,34 @@ public class FileUtils {
public static Uri getUriForFile(Context context, File 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;
}
}