Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Library Project - Error when calling context.getResources().openRawResource()
    primarykey
    data
    text
    <p>I'm developing an android library project that should read from an xml file in its raw resources (let's call it xml_file_name.myextension).</p> <p>What I do is basically creating a jar file of the library project including these folders:</p> <ul> <li>src</li> <li>gen</li> <li>lib</li> <li>res/raw</li> </ul> <p>and referencing it as a library in a test app. This is the code that I use (inside the library project) in order to get the xml file:</p> <pre><code>int xml_res_id = -1; for (Field f : R.raw.class.getFields()) { System.out.println("Raw resource found: " + f.getName()); if (f.getName().equalsIgnoreCase("xml_file_name")) xml_res_id = f.getInt(null); } if(xml_res_id != -1){ System.out.println("xml_file_id: " + xml_res_id); InputStream is = context.getResources().openRawResource(xml_res_id); // Decode xml file with SAXParser.. } </code></pre> <p>(I have the app context because the app explicitly passes it to the library project.) </p> <p>What happens is that when I launch the test app (and call the method that reads the xml file) I get this error:</p> <p><img src="https://i.stack.imgur.com/0fl8H.png" alt="Android Console Error"></p> <p>It seems that the xml file is actually in the right folder, because:</p> <p>1) The for loop actually prints "Raw resource found: xml_file_name.myextension" and "xml_file_id: 2130968576"</p> <p>2) If I put a file named "xml_file_name.myextension" in the res/raw folder of the app, it does not compile, and the error is: "Error generating final archive: Found duplicate file for APK: res/raw/xml_file_name.myextension". This basically gives me the proof that the file is correctly "imported" from the library project.</p> <p>Please Note: I also tried in this other way</p> <pre><code>InputStream is = context.getResources().openRawResource(R.raw.xml_file_name); </code></pre> <p>getting the same error.</p> <p>I honestly don't understand what could be the problem.. what am I doing wrong?</p> <hr> <h2>Edit:</h2> <p>for anyone interested in this issue: I finally realized that this is not possible, basically because when I try to get a resource through context.anymethod I refer to the R file of the app, so I can't give the resource ID got from the R file of my library project. </p> <p>It will compile, because the library project jar file contains the resource (R.raw.xml_file), but the call to context.something will always give null as a result because it refers to the app R file, that does not have that particular resource in it.</p> <p>I finally had to put my xml file in the res/raw folder of the app, and access the xml_file raw resource in this way:</p> <pre><code>int xml_id = context.getResources().getIdentifier("xml_file_name", "raw", context.getPackageName()); // Getting input stream from xml file InputStream is = context.getResources().openRawResource(xml_id); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload