- 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 .
- 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);