Search code examples
androidofflineosmdroid

OSMdroid - unable to load offline map tiles from custom location


I have looked around and can't find an answer to what I hope is quite a simple thing to achieve this question seemed similar but has no replies! So the problem is this; I have my offline map tiles on the android device here:

/sdcard/application-name/uuid/tiles

and I have set the OSM cache like so:

File applicationDir = new File(Environment.getExternalStorageDirectory(), File.separator + "application-name" + File.separator + "uuid" + File.separator);
OpenStreetMapTileProviderConstants.setCachePath(applicationDir.getAbsolutePath());

I then set up the TileSource:

ITileSource tileSource = new XYTileSource ("tiles", 0, 18, 256, ".png", new String[]{});

and the TileProvider:

tileProvider = new MapTileProviderBasic(getActivity());
tileProvider.setTileSource(tileSource);

When I run my app, I get a succession of these:

Error downloading MapTile: /15/16000/9960
java.lang.IllegalArgumentException: n <= 0: 0
at java.util.Random.nextInt(Random.java:175)
at org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase.getBaseUrl(OnlineTileSourceBase.java:33)
at org.osmdroid.tileprovider.tilesource.XYTileSource.getTileURLString(XYTileSource.java:19)
at org.osmdroid.tileprovider.modules.MapTileDownloader$TileLoader.loadTile(MapTileDownloader.java:171)
at org.osmdroid.tileprovider.modules.MapTileModuleProviderBase$TileLoader.run(MapTileModuleProviderBase.java:291)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

Presumably because the local tiles aren't found and the MapTileProviderBasic ultimately falls to the MapTileDownloader and it barfs because there are no online tiles.

Any suggestions gratefully received! Thanks.


Solution

  • So I eventually sorted this out by using the OSMDroid default location on the device

    /sdcard/osmdroid/uuid/tiles
    

    and set the tile source like so

    ITileSource tileSource = new XYTileSource(uuid, 0, 17, 256, ".png", new String[0]);
    

    osmdroid looks inside the directory uuid for a subdirectory called 'tiles', although the documentation kind of tells you this, it's not very explicit. I think you could probably use any location on the sdcard using this approach, I just decided to use the simplest solution.