我正在尝试加载位于android studio中/ assets文件夹中的.map文件。 .map文件是从mapsforge下载的 @覆盖 protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main);
map = (MapView) findViewById(R.id.map);
map.setBuiltInZoomControls(true);
map.setTileSource(TileSourceFactory.MAPNIK);
IMapController controller = map.getController();
GeoPoint point = new GeoPoint(41.2585, 69.2097);
controller.animateTo(point);
controller.setZoom(10);
File f = new File("file:///android_asset/" + "myfile.map");
if (f.exists()) {
File[] list = f.listFiles();
if (list != null) {
for (int i = 0; i < list.length; i++) {
if (list[i].isDirectory()) {
continue;
}
String name = list[i].getName().toLowerCase();
if (!name.contains(".")) {
continue; //skip files without an extension
}
name = name.substring(name.lastIndexOf(".") + 1);
if (name.length() == 0) {
continue;
}
if (ArchiveFileFactory.isFileExtensionRegistered(name)) {
try {
OfflineTileProvider tileProvider = new OfflineTileProvider(new SimpleRegisterReceiver(this),
new File[]{list[i]});
map.setTileProvider(tileProvider);
String source = "";
IArchiveFile[] archives = tileProvider.getArchives();
if (archives.length > 0) {
Set<String> tileSources = archives[0].getTileSources();
if (!tileSources.isEmpty()) {
source = tileSources.iterator().next();
map.setTileSource(FileBasedTileSource.getSource(source));
} else map.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
} else map.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
map.invalidate();
return;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
Toast.makeText(this, f.getAbsolutePath() + " did not have any files I can open! Try using MOBAC", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, f.getAbsolutePath() + " dir not found!", Toast.LENGTH_SHORT).show();
}
}
找不到错误“myfileDirectory”。
答案 0 :(得分:1)
由于我写了你发布的代码,我觉得我很喜欢。
为了节省您的时间,以下是maps
为File[]
的相关代码段。
XmlRenderTheme theme = null;
try {
theme = new AssetsRenderTheme(getContext().getApplicationContext(), "renderthemes/", "rendertheme-v4.xml");
} catch (Exception ex) {
ex.printStackTrace();
}
fromFiles = MapsForgeTileSource.createFromFiles(maps, theme, "rendertheme-v4");
forge = new MapsForgeTileProvider(
new SimpleRegisterReceiver(getContext()),
fromFiles, null);
mMapView.setTileProvider(forge);
答案 1 :(得分:1)
回答我自己的问题, * .map文件需要放在/ sdcard / osmdroid /
下致http://programtalk.com/java-api-usage-examples/org.osmdroid.mapsforge.MapsForgeTileProvider/
在OnCreate内部
mMap = (MapView) findViewById(R.id.map);
Set<File> mapfiles = findMapFiles();
File[] maps = new File[mapfiles.size()];
maps = mapfiles.toArray(maps);
if (maps.length==0)Log.d(TAG, "No Mapsforge files found");
else Toast.makeText(this, "Loaded " + maps.length + " map files",
Toast.LENGTH_LONG).show();
XmlRenderTheme theme = null;
try {
theme = new AssetsRenderTheme(getApplicationContext(),
"renderthemes/","rendertheme-v4.xml");
}catch (Exception ex){
ex.printStackTrace();
}
MapsForgeTileProvider forge = new MapsForgeTileProvider(new
SimpleRegisterReceiver(this), MapsForgeTileSource.createFromFiles(maps,
theme, "rendertheme-v4"), null);
mMap.setTileProvider(forge);
mMap.setUseDataConnection(false);
mMap.setMultiTouchControls(true);
mMap.setBuiltInZoomControls(true);
获取.map文件的方法
protected static Set<File> findMapFiles() {
Set<File> maps = new HashSet<>();
List<StorageUtils.StorageInfo> storageList =
StorageUtils.getStorageList();
for (int i = 0; i < storageList.size(); i++) {
File f = new File(storageList.get(i).path + File.separator +
"osmdroid" + File.separator);
if (f.exists()) {
maps.addAll(scan(f));
}
}
return maps;
}
static private Collection<? extends File> scan(File f) {
List<File> ret = new ArrayList<>();
File[] files = f.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.getName().toLowerCase().endsWith(".map"))
return true;
return false;
}
});
if (files != null) {
for (int i = 0; i < files.length; i++) {
ret.add(files[i]);
}
}
return ret;
}
答案 2 :(得分:0)
如果OfflineTileProvider
无法从InputStream
读取但需要File
对象到.map文件,那么您应该首先关注真实文件。
因此请复制所有文件&#39;首先从资产到文件系统上的文件。
之后您可以使用这些文件。
将文件从资产复制到文件存储的代码已经在这里发布了一百次。