0
  1. I am able to download a database file well from the server to the sdcard but now the problem is when the download is interrupted it does not resume thus the database file downloaded is incomplete thus crashing my app. Is their a way i can resume my download or compare the size of the downloaded file with the original file size .
  2. How can i get the server date or the network operator date for another purpose of comparison

sample code

  protected String doInBackground(String... f_url) {
        int count;
        try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();
            // getting file length
           int lenghtOfFile = conection.getContentLength();

            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream to write file
            String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
            File testDirectory = new File(baseDir +"/test");
             testDirectory.mkdirs();

            OutputStream output = new FileOutputStream(testDirectory + "/db");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress(""+(int)((total*100)/lenghtOfFile));

                // writing data to file
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }

    /**
     * Updating progress bar
     * */
    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
        pDialog.setProgress(Integer.parseInt(progress[0]));
   }

Am just calling where file_url is the download link

  new DownloadFileFromURL().execute(file_url);
Simon Macha Mwas
  • 317
  • 2
  • 3
  • 9
  • These are two questions. I advise you to post them in separate threads. What class you use for the downloading? Is it `HttpClient`? – Boris Strandjev Jan 12 '13 at 16:54
  • Here's a SO question with an answer to resuming download; http://stackoverflow.com/questions/6237079/resume-http-file-download-in-java – harism Jan 12 '13 at 17:04

0 Answers0