2016-09-13 10 views
0

インターネットからファイルをダウンロードしようとしています。それはうまく動作しますが、ファイルが2MBより大きい場合は、このエラーが発生します。あなたの助けをありがとうAndroidダウンロードファイル

09-13 17:49:05.228 17994-18110/? E/dalvikvm-heap: Out of memory on a 286-byte allocation. 
09-13 17:49:06.322 17994-18111/? E/dalvikvm-heap: Out of memory on a 16-byte allocation. 
09-13 17:49:07.361 17994-17994/? E/dalvikvm-heap: Out of memory on a 72-byte allocation. 
09-13 17:49:08.408 17994-18003/? E/dalvikvm-heap: Out of memory on a 24-byte allocation. 
09-13 17:49:09.447 17994-18003/? E/dalvikvm-heap: Out of memory on a 28-byte allocation. 
09-13 17:49:09.994 17994-18111/? E/dalvikvm-heap: Out of memory on a 28-byte allocation. 
09-13 17:49:10.541 17994-17994/? E/dalvikvm-heap: Out of memory on a 28-byte allocation. 

public static byte[] is2Bytes(InputStream is) { 
    byte[] buf = null; 
    BufferedInputStream bufIS = null; 
    if (is != null) 
     try { 
     ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); 
     bufIS = new BufferedInputStream(is); 
     buf = new byte[4096]; 
     int cnt; 
     while ((cnt = bufIS.read(buf)) >= 0) { 
      byteBuffer.write(buf, 0, cnt); 
     } 
     buf = byteBuffer.size() > 0 ? byteBuffer.toByteArray() : null; 
    } catch (Exception ignore) { 
    } finally { 
     try { 
      if (bufIS != null) bufIS.close(); 
     } catch (Exception ignore) { 
     } 
    } 
    return buf; 
} 

答えて

1

ファイル全体をメモリにダウンロードしようとしています。大容量の連続したRAMブロックに対してヒープスペースを持たないため、これは大きなファイルでは機能しません。

代わりにファイルをディスクに書き込むか、チャンク内のメモリを使用します(4Kをダウンロードし、4Kを消費し、4Kを破棄して繰り返します)。

+0

例がありますか? – user2847219

+0

@ user2847219:https:/// L73 – CommonsWare

関連する問題