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?