Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected Void doInBackground(String... params) {
- InputStream inputStream = null;
- FileOutputStream fileOutput = null;
- try {
- URL url = new URL(params[0]);
- File file = new File(path);
- URLConnection urlConnection = url.openConnection();
- inputStream = urlConnection.getInputStream();
- fileOutput = new FileOutputStream(file);
- int totalSize = urlConnection.getContentLength();
- int downloadedSize = 0;
- byte[] buffer = new byte[16384];
- int bufferLength = 0;
- while ((bufferLength = inputStream.read(buffer)) > 0 ) {
- while(isPaused) {
- sleep();
- }
- if(isCancelled()) {
- if(file.exists())
- file.delete();
- return null;
- }
- fileOutput.write(buffer, 0, bufferLength);
- downloadedSize += bufferLength;
- publishProgress(downloadedSize, totalSize);
- }
- if(totalSize > getFreeMemorySize()) {
- //if(true) {
- if(errorHandler != null)
- errorHandler.onMemorySizeException();
- cancel(true);
- }
- } catch (IOException e) {
- int i = e.hashCode();
- e.getStackTrace();
- }
- finally {
- try {
- if(inputStream != null)
- inputStream.close();
- if(fileOutput != null)
- fileOutput.close();
- } catch (IOException e) {
- int i = e.hashCode();
- e.getStackTrace();
- }
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement