0

I'm doing a project which is similar to the Download Manager.

One of the functions is to allow the user to pause the file so they can resume the download later. I have Googled it, but unluckily still haven't solved the problem (maybe I'm new in android and stupid to solve it).

The question is... If one of the files hasn't finish the download, how do I get the current stream / size of the file? And how can I continue to write the file?

I have seen 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 don't really understand how it works 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

xarlymg89
  • 2,552
  • 2
  • 27
  • 41
WynixToo
  • 161
  • 4
  • 15

1 Answers1

0

You can refer my answer to this question here

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

Community
  • 1
  • 1
naikus
  • 24,302
  • 4
  • 42
  • 43
  • 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 Aug 06 '10 at 01:27
  • 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 Aug 06 '10 at 02:37