我想加載放置在android studio的/ assets文件夾中的.map文件。 .map文件從mapsforge下載 @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);OSMDROID - 從mapsforge下載加載的.map文件
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」找不到。
'文件f =新的文件( 「文件:/// android_asset /」 + 「myfile.map」);'。這不起作用,因爲File類無法處理來自資產的文件。 Google如何從資產中讀取文件。 – greenapps
'if(f.exists())File [] list = f.listFiles();'。如果'f'是你的。地圖文件,那麼你怎麼能認爲你可以列出文件?你不是按照你所說的那樣加載那個文件,而是把它當作一個目錄來處理。 – greenapps
我試着從InputStream中讀取資產,但在這裏我需要它是文件不inputStream,我應該讀取它後將inputStream轉換爲文件? – MJahongir