Fix import gpx files to custom folders

(cherry picked from commit caac552822)
This commit is contained in:
Chumva 2019-07-17 17:20:38 +03:00
parent 0e965dd796
commit e7bb115e29
2 changed files with 17 additions and 4 deletions

View file

@ -2,8 +2,6 @@ package net.osmand.util;
import net.osmand.IProgress;
import net.osmand.PlatformUtil;
import net.osmand.router.GeneralRouter;
import net.osmand.router.RoutingConfiguration;
import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParser;
@ -31,7 +29,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Stack;
import java.util.zip.GZIPInputStream;
@ -370,6 +367,14 @@ public class Algorithms {
return "";
}
public static void createParentDirsForFile(File file) {
if (file != null && !file.exists()) {
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
}
}
@SuppressWarnings("TryFinallyCanBeTryWithResources")
public static void fileCopy(File src, File dst) throws IOException {

View file

@ -74,7 +74,6 @@ import net.osmand.plus.audionotes.AudioVideoNotesPlugin;
import net.osmand.plus.dialogs.ConfigureMapMenu;
import net.osmand.plus.helpers.ColorDialogs;
import net.osmand.plus.helpers.ExternalApiHelper;
import net.osmand.plus.helpers.LockHelper;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.other.IContextMenuButtonListener;
import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
@ -1315,6 +1314,9 @@ public class OsmandAidlApi {
File destination = app.getAppPath(IndexConstants.GPX_INDEX_DIR + destinationPath);
if (destination.getParentFile().canWrite()) {
boolean destinationExists = destination.exists();
if (!destinationExists) {
Algorithms.createParentDirsForFile(destination);
}
try {
Algorithms.fileCopy(source, destination);
finishGpxImport(destinationExists, destination, color, show);
@ -1336,6 +1338,9 @@ public class OsmandAidlApi {
gpxParcelDescriptor = app.getContentResolver().openFileDescriptor(gpxUri, "r");
if (gpxParcelDescriptor != null) {
boolean destinationExists = destination.exists();
if (!destinationExists) {
Algorithms.createParentDirsForFile(destination);
}
FileDescriptor fileDescriptor = gpxParcelDescriptor.getFileDescriptor();
InputStream is = new FileInputStream(fileDescriptor);
FileOutputStream fout = new FileOutputStream(destination);
@ -1370,6 +1375,9 @@ public class OsmandAidlApi {
InputStream is = new ByteArrayInputStream(sourceRawData.getBytes());
FileOutputStream fout = new FileOutputStream(destination);
boolean destinationExists = destination.exists();
if (!destinationExists) {
Algorithms.createParentDirsForFile(destination);
}
try {
Algorithms.streamCopy(is, fout);
finishGpxImport(destinationExists, destination, color, show);