编程语言
首页 > 编程语言> > java – 如何在我的Android应用程序中访问资产文件夹?

java – 如何在我的Android应用程序中访问资产文件夹?

作者:互联网

我正在为一位朋友写一个小项目.记事卡应用程序.
我的计划是将记录卡放在xml格式中,以便我可以轻松导入和导出它们.
我将xml文件放在assets / xml / mynotecard.xml文件夹中,但我无法设法访问此文件.

每当我尝试解释xml文件(将在以后的类中放入它)时,我得到异常:test.xml不是文件.
这是我的代码的摘录:

公共类NotecardProActivity扩展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();
}

}

如果你们可以帮助我真的很棒:)

解决方法:

压缩您的xml文件夹(例如xml.zip)并将其放入资产中.使用AssetsManager获取资源非常慢.此外,这样做可以维护目录结构,只需将其复制到SD卡并在那里解压缩即可.这比获取目录中的每个文件要快得多.您可以使用此帖子进行解压缩:How to unzip files programmatically in Android?.希望这会有所帮助.

标签:java,android,android-assets
来源: https://codeday.me/bug/20190704/1376310.html