Search code examples
androiddownloadfileoutputstreamion-koush

Android ION download with progressbar using FileOutputStream


I'm trying to modify koush's code from download sample with progressbar, to make it write to a FileOutputStream instead of File, but eclipse give me the following error:

The method progressHandler(new ProgressCallback(){}) is undefined for the type ResponseFuture

Here is the code:

File file = new File(DownloadPath, uri.getLastPathSegment());
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
}

Future<FileOutputStream> downloading = Ion.with(getApplicationContext())
    .load(uri)
    .write(fos)
    .progressHandler(new ProgressCallback() { 
        @Override
        public void onProgress(int downloaded, int total) {
            // inform the progress bar of updates in progress
        }
    })
    .setCallback(new FutureCallback<FileOutputStream>() {
       @Override
        public void onCompleted(Exception e, FileOutputStream file) {
            // download done...
            // do stuff with the File or error
        }
    });     

Solution

  • You seem to have messed up with the order. Please try this:

    Future<FileOutputStream> downloading = Ion.with(getApplicationContext())
        .load("http://example.com/test.txt")
        .progressHandler(new ProgressCallback() { 
            @Override
            public void onProgress(int downloaded, int total) {
                // inform the progress bar of updates in progress
            }
        })
        .write(fos)
        .setCallback(new FutureCallback<FileOutputStream>() {
           @Override
            public void onCompleted(Exception e, FileOutputStream file) {
                // download done...
                // do stuff with the File or error
            }
        });