I have developed an app in which I have an ASyncTask that downloads a file from Amazon s3 server. As the related files are quite large sized, I need to show a progress bar when the download happens and a spiral progress bar doesn't look good here when it keeps rotating and irritates the user. I need to use a horizontal style progress bar for which I need to know the size of the file to be downloaded. Does anybody know of any way to know the file size before downloading it from amazon s3.
This is the downloading code that I have used..
@Override
protected Void doInBackground(Void... params) {
TransferManager manager = new TransferManager(access);
File file = new File(Environment.getExternalStorageDirectory()+"/Downloads", downFile);
fileDownloaded = "/Downloads/"+ downFile;
file.setWritable(true);
Download down = manager.download("files", fileToDown, file );
try {
down.waitForCompletion();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
'downFile' is the file location where I am saving it on external directory. 'fileToDown' is the file path on server that is to be downloaded. '/Downloads/' is the new directory that I have created to store files on device.
I would assume you are using a code something like this,
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
// getting file length
int lengthOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
the method getContentLength()
will return to you the size of the file.
UPDATE 1:
I checked the java docs of Amazon about the TransferManager and Download class. There were method and listener that might be useful to display your progress and try using them.
getProgress() //This will return TransferProgress object. See the links above
addProgressListener
This will able you I think to set up a progressListener.
UPDATE 2:
TransferProgress is an object which has bunch of informations about the progress of your download. It has bytes transferred, total bytes transferred and transfer percentage. Look at the methods detail.