Android load local xml file

谁说我不能喝 提交于 2020-03-06 03:34:24

问题


I'm working with Eclipse. Does anybody know where this piece of code tries to get the file from? I've copied my files to the project folder and also the srr/package_path folder.

public String getFromFile(ASerializer aserializer) {
    String data = new String();
    try {
    FileInputStream stream = new FileInputStream(new File(aserializer.toLocalResource()));
       FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        data  = Charset.defaultCharset().decode(bb).toString();
        stream.close();
    }
    catch (Exception e){
        data = "File not found";
    }
    return data;
}

回答1:


That code, because of its use of FileInputStream, will interpret the file path as a path to a file on the whole Android file system. That is, the file system root will be the root of the file system in which the whole Android system resides rather than somewhere in your application resources.

If you want to load files from your application resources based on file name, you can put the files in the 'assets' directory of your Eclipse application project and then use:

InputStream inputStream = getResources().getAssets().open("file.xml");

to get an InputStream of the file to work with. The path given to AssetManager.open() is relative to the 'assets' directory. You can also organize files in sub directories:

InputStream inputStream = getResources().getAssets().open("subdirectory/file.xml");



回答2:


i think its either the assets folder or the res folder i would suggest using the DDMS file explorer to find where exactly

in eclipse go to DDMS once in there go to the file explorer tab you should find your application in data/data/package name (could be slightly off this is from memory) search the folders within your package name



来源:https://stackoverflow.com/questions/7821636/android-load-local-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!