Should I use something other than getResource (). GetStringArray () to populate a large array?

Following the sample Activity.getResource (). getStringArray () :

String [] mDefinitions = getResources().getStringArray(R.array.definition_array); 

The documentation for this method is pretty clear, and I did not expect any problems with it:

Returns an array of strings associated with a specific resource identifier.

This approach worked as expected for small datasets. However, when I populated strings.xml with a full set of data (about 2,000 entries), I found that the application crashed while trying to load a resource. I noticed this error in the console log:

ReferenceTable overflow (max = 512)

Playing with the number of elements in my string array, I confirmed that the error is reproducible when the number of elements has exceeded ~ 700 records.

As a result of the problem, there were some examples of other developers with the same problem , but I can not find anything in the Android documentation to explain this.

Someone had a problem creating a problem for the problem on the Google Code Google page, but neither she nor any of the messages I came across got a satisfactory answer.

Am I getting the problem wrong? Should I fill in the data myself (file upload and JSON parsing or similar) and completely eliminate the problem? It seems to me that I do not see anything obvious here.

+4
source share
2 answers

I need to be able to parse large XML files for my specific application (I already had data encoded in this way and I wanted to keep it consistent).

So, my first attempt was to use getStringArray, which suffers from the problem described in the question:

 String [] mDefinitions = getResources().getStringArray(R.array.definition_array); 

My second attempt has the same limitations as me with getStringArray. As soon as I tried to process a larger XML file (> 500K), I got DalvikVM theft on getXml:

  XmlResourceParser parser = getResources().getXml(R.xml.index); try { int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { String name = null; switch (eventType){ case XmlPullParser.START_TAG: // handle open tags break; case XmlPullParser.END_TAG: // handle close tags break; } eventType = parser.next(); } } catch (XmlPullParserException e) { throw new RuntimeException("Cannot parse XML"); } catch (IOException e) { throw new RuntimeException("Cannot parse XML"); } finally { parser.close(); } 

My final solution using the SAX parser for an InputStream created from a raw resource. I can parse large XML files without crashing DalvikVM:

  InputStream is = getResources().openRawResource(R.raw.index); XmlHandler myXMLHandler = new XmlHandler(); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); xr.setContentHandler(myXMLHandler); xr.parse(new InputSource (is)); } catch (Exception e) { System.out.println("XML Pasing Excpetion = " + e); } 

Where is XmlHandler:

  public class XmlHandler extends DefaultHandler { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // handle elements open } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // handle element close } @Override public void characters(char[] ch, int start, int length) throws SAXException { // handle tag characters <blah>stuff</blah> } } 
+6
source

Not sure what the purpose of your application is, but you thought about using the csv file.

  InputStream is = c.getResources().openRawResource(R.raw.csv_file); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String readLine = null; int column = 1; try { while ((readLine = br.readLine()) != null) { } } catch (IOException e) { e.printStackTrace(); } 
+1
source

All Articles