4

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 :)

McDowell
  • 107,573
  • 31
  • 204
  • 267
dhartwich
  • 398
  • 2
  • 6
  • 16

3 Answers3

2

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.

Community
  • 1
  • 1
Avezou
  • 91
  • 1
  • 6
  • Have you found another solution to this? If so, you should put it here as an answer. If my answer helped, and if you don't mind, could you please mark it as an accepted answer? – Avezou Nov 05 '15 at 00:08
0

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

TacB0sS
  • 10,106
  • 12
  • 75
  • 118
  • so would it be like /root/assets/xml/test.xml ? Also, what would be a good workaround ? because i like the folder structure of assets and i dont want my xml files to lay around in the res folder :( – dhartwich Apr 22 '12 at 11:42
  • no solution as far as I know... it is a limitation. also another issue is that assets are not aggregated in project dependencies, means: if you have an apklib with assets in it, these assets would not be aggregated to the dependent project. – TacB0sS Apr 22 '12 at 11:44
  • what can i do then to fix my problem ? is there any workaround ? – dhartwich Apr 22 '12 at 11:45
  • No workaround that I know of. I've just used the assets with what they are capable of. – TacB0sS Apr 22 '12 at 11:49
  • I mentioned it in previous comments. If you zip the directory structure containing your files, then you can just put that zip in the root (assets). Then you will do one read from the assets (getting files from the asset manager is slow), then unzip it on the device. Information about unzipping can be found [here](http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android). – Avezou Jan 07 '16 at 20:56
0
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());
    }
} 
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • this gets you also files in sub folders? – TacB0sS Apr 22 '12 at 12:56
  • what you need all files or single file? – Shankar Agarwal Apr 22 '12 at 12:57
  • would love to have all my files in a list to have them available later when i need to translate the xml files into strings and display them in my app – dhartwich Apr 22 '12 at 13:32
  • files = assetManager.list(""); doesn't work- i changed it into files = Arrays.asList(assetManager.list("")); but now i have the problem that it displays my xmls and also images, sounds and webkit . Also, how do i convert these strings into my xml files ? – dhartwich Apr 22 '12 at 13:52
  • `code` ArrayList filesa = new ArrayList(); List str = getFiles(); for (int i=0; i<= str.size()-1;i++) { filesa.add(new File(str.get(i))); Log.v("NeueListe: ", str.get(i)); } for (int i=0; i<=filesa.size()-1;i++) { Log.v("FileArrayneu: ", filesa.get(i).getAbsolutePath()); Log.v("FileArrayneu: ", filesa.get(i).getName()); } `code` tried this but now i get the message, that the protocal can not be found :/ – dhartwich Apr 22 '12 at 13:59
  • you should update your post and not reply this in a comment... :) – TacB0sS Apr 23 '12 at 07:09