How to access assets folder in my android app?

限于喜欢 提交于 2019-12-01 05:22:22

问题


I'm writing at a little project for a friend. A notecard application. My plan is to put the notecards in an xml format so i can import and export them easily. I located the xml files at the assets/xml/mynotecard.xml folder but i just can't manage to get access to this file.

Whenever i try to interpret the xml file (will be put to it's on class later) i get the exception with: test.xml is not a file. This is an extract of my code:

public class NotecardProActivity extends Activity {

List<String> xmlFiles;
public ArrayList<File> xmlFileList;
XMLInterpreter horst;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   final AssetManager mgr = getAssets();


    displayFiles(mgr, "",0); 
     xmlFiles = displayFiles(mgr, "xml",0); 
     for (int e = 0; e<=xmlFiles.size()-1;e++)
     {
        Log.v("Inhalt List"+e+": ", xmlFiles.get(e));
     }     

    xmlFileList = new ArrayList<File>();
    for (int i = 0; i<=xmlFiles.size()-1;i++)
    {

        xmlFileList.add(new File("./assets/xml/"+xmlFiles.get(i)));
    }
    for (int i = 0; i<=xmlFileList.size()-1;i++)
    {
        Log.v("Name: ", xmlFileList.get(i).getName());
        Log.v("Filelocation: ",xmlFileList.get(i).getPath());
        Log.v("Filelocation: ",xmlFileList.get(i).getAbsolutePath());               
    }
    Log.v("DEBUG","XML FILE LISTE erfolgreich geschrieben!");

    //Alternative zum ausgelagerten XML INTERPRETER
    try
    {
    if(xmlFileList.get(0) == null){
        Log.v("Debug", "XMLFILE IS NULL");
    }
    else{
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFileList.get(0));
    doc.getDocumentElement().normalize();

    Log.v("Root element :", doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("notecard");


    for (int temp = 0; temp < nList.getLength(); temp++) {

           Node nNode = nList.item(temp);
           if (nNode.getNodeType() == Node.ELEMENT_NODE) {

              Element eElement = (Element) nNode;    
              Log.v("GesetzesText: " , getTagValue("legal_text", eElement));
             // System.out.println("Kommentar : " + getTagValue("comment", eElement));                   
           }
        }
    }
    }
    catch (Exception e) {
        e.printStackTrace();
      }

    mgr.close();                
}


private List<String> displayFiles (AssetManager mgr, String path, int level) {
    List<String> abc = new ArrayList<String>();
    Log.v("Hello","enter displayFiles("+path+")");
   try {
       String list[] = mgr.list(path);
       abc.addAll(Arrays.asList(list));
        Log.v("Hello1","L"+level+": list:"+ Arrays.asList(list));

   } catch (IOException e) {
       Log.v("Fail","List error: can't list" + path);
   }
return abc;

}

private String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

    Node nValue = (Node) nlList.item(0);

    return nValue.getNodeValue();
}

}

Would be really awesome if you guys could help me :)


回答1:


Zip your xml folder (xml.zip for example) and put it in assets. Getting assets with AssetsManager is very slow. Also, doing so you can maintain your directory structure and just copy it to sdcard and extract it there. This will be a lot faster than getting each file in the directory. You can use this post for the unzipping: How to unzip files programmatically in Android?. Hope this helps.




回答2:


Assets should be in the root of the folder... cannot put file in a sub folder. assets\file.suf would work while assets\sub\file.suf would not




回答3:


android.resource://packagename/assets/filename

you can use the above line to refer to a file in assets folder. Just replace the package name and file name.

Note file name must not contain extensions

Updated::

private List<String> getFiles(){
 AssetManager assetManager = getAssets();
    List<String> files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }
} 


来源:https://stackoverflow.com/questions/10267594/how-to-access-assets-folder-in-my-android-app

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