diff --git a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java index 3e6caffafa..cd32d5e0f3 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java @@ -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 { diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 07da3f1642..c67f63d588 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -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);