我想保存一個非常大的在線xml文件(大約33mb)。我想在StringBuilder中獲取xml文件,將其轉換爲字符串,然後通過FileOutputStream將文件保存在內部存儲/ Sdcard中。Android - 保存大型Xml文件(內部/ SD卡)
但我越來越內存和應用程序崩潰。當我嘗試從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());
}
試着這樣做,會讓你知道結果。 – ZealDeveloper 2012-04-23 09:34:08
完美無瑕。感謝名單 – ZealDeveloper 2012-04-23 10:10:14