I am writing my first application, so please do not scold too much ...

Is it possible to download a file from the Internet without the help of BufferedInputStream ?

The problem is that when you download a file up to 50 MB in size, everything is fine, but if the file is larger, I get an exception with an error about heap overflow.

12-18 18: 20: 26.846: I / dalvikvm-heap (12319): Forcing collection of SoftReferences for 58369314-byte allocation __12-18 18: 20: 26.916: E / dalvikvm-heap (12319): Out of memory on a 58369314-byte allocation.

Everything else, the limit of the file varies depending on the device.

Code:

  @Override protected Boolean doInBackground(String... params) { //В этом методе происходит загрузка файла через //стандартный класс URLConnection int count; try { int i=0; dataFiles = getResources().getStringArray(R.array.dataFiles); File path = new File(Environment.getExternalStorageDirectory()+"/testing"); if(!path.exists()){ path.mkdirs(); } for (String url_string : params) { URL url = new URL(url_string); URLConnection conection = url.openConnection(); conection.connect(); int lenghtOfFile = conection.getContentLength(); InputStream input = new BufferedInputStream(url.openStream(), lenghtOfFile); OutputStream output = new FileOutputStream(path+"/"+dataFiles[i]); byte data[] = new byte[256]; long total = 0; while ((count = input.read(data)) != -1) { //Проверяем, актуальна ли еще задача if (isCancelled()){ output.flush(); output.close(); input.close(); return null; } total += count; output.write(data, 0, count); //Информирование о закачке. //Передаем число, отражающее процент загрузки файла //После вызова этого метода автоматически будет вызван //onProgressUpdate в главном потоке publishProgress((int)((total*100)/lenghtOfFile)); } output.flush(); output.close(); input.close(); ++i; } } catch (Exception e) { System.out.println("Ошибка: "+e.getMessage()); } return true; } 

Tell me, please, how is it possible to download large files without having to eat the whole bunch?

  • @ Gordon1945, If you are given a comprehensive answer, mark it as correct (click on the check mark next to the selected answer). - Nicolas Chabanovsky
  • Thank you so much for trying. But everything turned out to be much simpler: It was necessary to change it with BufferedInputStream (url.openStream (), lenghtOfFile); on BufferedInputStream (url.openStream (), "1024"); So I limited the size of the heap requested, instead of requiring a file-size buffer. - Gordon1945

2 answers 2

It’s not clear why you use the BufferedInputStream when loading fixed-size data from the network, and saving it immediately to a file, which makes no sense, and you don’t wrap the FileOutputStream into a BufferedOutputStream , which should significantly reduce the brakes, in the case of a slow FS, and fast Internet connections. Try the opposite.

    When I used it in my application, I did something like this DOWNLOAD LARGE FILES . Try this code, if it doesn't work out - write, think up something. Now there is no way to check the code.