Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfMemoryError while converting Stream to String in Android

am loading xml file from Assets folder. am getting OutOfMemoryError. The code which i have used is

private String convertStreamToString(InputStream is) {  
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + NEW_LINE);
        }
        reader.close();

    } catch (IOException e) {
        //do nothing.
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            //do nothing.    
        }
    }
    reader=null;
    return sb.toString();
}

Is there an alternate way to get rid of this Exception. It will be more helpful if you post any code. Thanks in advance.

like image 292
chipmunk Avatar asked Feb 03 '26 22:02

chipmunk


1 Answers

It's not a good idea to parse a big Xml using a String. You should turn to a streaming version of the parser. Google Http Java Client proposes such a library : http://code.google.com/p/google-http-java-client

like image 163
Snicolas Avatar answered Feb 06 '26 10:02

Snicolas