views:

87

answers:

1

Hi, Im doing a project which similiar like download manager. I have 1 of the function is it allow user to pause the file and they can resume the download later.I have google it, but unlucky i stil haven solve the problem (maybe i'm new in android and stupid to solve it).

the question is.. if one of the file was haven finish downloaded for internet, how to i get the current stream/size of the file? and how can i continues writting the file?

i have saw a code in java as below:

begin = downloaded;
file = (FileConnection) Connector.open("\sdcard\abc.mp3");
OutputStream output = file.openOutputStream(begin);
DataOutputStream dos = new DataOutputStream(output);
if(read != -1) {
dos.write(buffer, 0, read);
dos.flush();
    downloaded += read;
}

But i'm not really understand how it work in android :( Your reply is very appreciated!!!

P/S: I'm sorry about my bad english, hope you guys understand what i'm talking about. Thank you.

Regard

Wynix

A: 

You can refer my answer to this question here

It talks about partial download and resuming downloads. Hope it helps

naikus
Hi naikus, thanks for reply. the resuming donwload was done. but how about if the file was downloaded in half and the application was closed? how to i continues writing the file in start from half? because when the application restart all the activity suppose to restarted right? isn't i should get the file size/stream and then use "outputstream.write(buffer, downloadedStream, end);" this methods? thank you.
WynixToo
You can name the file that is "being" downloaded as "name.part" and a corrosponding text file filename.url (to store download url) e.g. bigfile.tar.part. When your application starts, check for any ".part" files in your download dir. To resume a download, get the length of the .part file as "len", get the URL from the url file, do "range = len(bytes) - 500(bytes)", use the "range" in the http header, Then you can use RandomAccessFile("rw") to seek(range) and start writing.
naikus
Finally i done the resuming download. Thanks naikus!!! u help me alot.
WynixToo