如何获取Android路径字符串资产文件夹中的文件? [英] How to get the android Path string to a file on Assets folder?
问题描述
我需要知道的字符串路径以资产文件夹中的文件,因为我使用需要接收一个字符串路径的地图API,而我的地图必须存储资产的文件夹
这是在code我试图:
图形页面图形页面=新的图形页面(本);
mapView.setClickable(真正的);
mapView.setBuiltInZoomControls(真正的);
mapView.setMapFile(文件:///android_asset/m1.map);
的setContentView(图形页面);
东西与脚麻文件:///android_asset/m1.map
,因为没有被加载地图
至极是正确的路径字符串的文件存储在我的资产文件夹中的文件m1.map?
感谢
编辑Dimitru:此code不工作,它不能对 is.read(缓冲);
与IOException异常
尝试{
InputStream的是= getAssets()开(m1.map)。
INT大小= is.available();
byte []的缓冲区=新的字节[尺寸]
is.read(缓冲液);
is.close();
文=新的String(缓冲区);
}赶上(IOException异常E){抛出新的RuntimeException(E);}
AFAIK在资产目录中的文件不要解压。 相反,它们是直接从APK(ZIP)文件读取
所以,你真的不能做的东西希望获得的文件的接受资产'文件'。
相反,你必须提取的资产,并将其写入到一个单独的文件,就像杜米特鲁提示:
文件f =新的文件(getCacheDir()+/ m1.map);
如果(!f.exists()){尝试
InputStream的是= getAssets()开(m1.map)。
INT大小= is.available();
byte []的缓冲区=新的字节[尺寸]
is.read(缓冲液);
is.close();
FileOutputStream中FOS =新的FileOutputStream(F);
fos.write(缓冲液);
fos.close();
}赶上(例外五){抛出新的RuntimeException(E); }
mapView.setMapFile(f.getPath());
I need to know the string path to a file on assets folder, because i'm using a map api that needs to receive a string path, and my maps must be stored on assets folder
This is the code i'm trying:
MapView mapView = new MapView(this);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setMapFile("file:///android_asset/m1.map");
setContentView(mapView);
Something is going wrong with "file:///android_asset/m1.map"
because the map is not being loaded.
Wich is the correct string path file to the file m1.map stored on my assets folder?
Thanks
EDIT for Dimitru: This code doesn't works, it fails on is.read(buffer);
with IOException
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 the files in the assets directory don't get unpacked. Instead, they are read directly from the APK (ZIP) file.
So, you really can't make stuff that expects a file accept an asset 'file'.
Instead, you'll have to extract the asset and write it to a seperate file, like Dumitru suggests:
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());
这篇关于如何获取Android路径字符串资产文件夹中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!