我需要知道字符串路径到资产文件夹中的文件,因为我使用的地图 API 需要接收字符串路径,并且我的地图必须存储在资产文件夹中
这是我正在尝试的代码:
MapView mapView = new MapView(this);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setMapFile("file:///android_asset/m1.map");
setContentView(mapView);
出了问题"file:///android_asset/m1.map"
因为地图尚未加载。
哪个是文件的正确字符串路径m1.map存储在我的资产文件夹中?
Thanks
Dimitru 的编辑:此代码不起作用,它失败了is.read(buffer);
with IO异常
try {
InputStream is = getAssets().open("m1.map");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
text = new String(buffer);
} catch (IOException e) {throw new RuntimeException(e);}
AFAIK 资产目录中的文件不会被解压。
相反,它们是直接从 APK (ZIP) 文件中读取的。
所以,你真的无法制造出期望的东西file接受资产“文件”。
相反,您必须提取资产并将其写入单独的文件,就像 Dumitru 建议的那样:
File f = new File(getCacheDir()+"/m1.map");
if (!f.exists()) try {
InputStream is = getAssets().open("m1.map");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (Exception e) { throw new RuntimeException(e); }
mapView.setMapFile(f.getPath());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)