2012-04-23 1 views
0

크기가 매우 큰 온라인 XML 파일 (약 33MB)을 저장하고 싶습니다. StringBuilder에서 xml 파일을 가져 와서 문자열로 변환 한 다음 FileOutputStream에 의해 내부 저장소/Sdcard에 파일을 저장하려고합니다.Android - 큰 XML 파일 저장 (내부/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()); 
    } 

답변

2

영혼의 문제는 xml 문자열이 메모리에 완전히 채워지기 때문에 많은 app-memory가 필요하다는 것입니다.

이 같은 litte 1킬로바이트 덩어리의 데이터를 처리하여이를 방지 할 수 있습니다

is = httpEntity.getContent(); 

    FileOutputStream fos = openFileOutput("test.xml", Context.MODE_PRIVATE); 

    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = is.read(buffer))>0){ 
     fos.write(buffer, 0, length); 
    } 

    fos.flush(); 
    fos.close(); 
    is.close(); 
+0

이것을 시도해도 결과를 알려줍니다. – ZealDeveloper

+0

완벽하게 작동합니다. 고맙습니다. – ZealDeveloper

1

아래 코드를 사용해 볼 수 : 여기

내 현재 코드인가?

BufferedReader reader = new BufferedReader(new InputStreamReader(
     is, "iso-8859-1"), 8); 
FileOutputStream fos = openFileOutput("test.xml", Context.MODE_PRIVATE); 
StringBuilder sb = new StringBuilder(); 
String line = null; 
while ((line = reader.readLine()) != null) { 

    sb.append(line + "\n"); 

    if (sb.toString().length() > 10000) { 
     fos.write(sb.toString().getBytes()); 
     fos.flush(); 
     sb = new StringBuilder(); 
    } 
} 
is.close(); 

fos.close(); 
+0

내가 반드시이 시도됩니다, u는 최대한 빨리 알려 드릴 것입니다. – ZealDeveloper

+0

이것은 완벽하게 작동하지만 @ k3b 방법이 이보다 잘 작동하는 것 같습니다. 대답은 고맙습니다. – ZealDeveloper