【发布时间】:2012-04-23 09:04:02
【问题描述】:
我想保存一个非常大的在线 xml 文件(大约 33mb)。我正在尝试在 StringBuilder 中获取 xml 文件,将其转换为字符串,然后通过 FileOutputStream 将文件保存在内部存储/Sdcard 中。
但是我的内存不足,应用程序崩溃了。当我尝试从 StringBuilder 获取字符串中的值时发生崩溃。
这是我当前的代码:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("sorry cant paste the actual link due copyrights.xml");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String result = sb.toString();
System.out.println(result);
FileOutputStream fos = openFileOutput("test.xml", Context.MODE_PRIVATE);
fos.write(sb.toString().getBytes());
fos.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
【问题讨论】:
标签: android xml parsing sd-card out-of-memory