I'm having trouble of looking for a way to know whether a file exists, if it does, does it have the same content? If yes then don't download, otherwise download the file.
In my code, I need to download the PDF file before viewing it. I have already the checking if file exists, but it checks only the filename (this one I'm not sure of). Does the File
class' exists()
method only check for filename? If it does, how do I know if it has different content?
Here's my code:
class DownloadFileTask extends AsyncTask<String, String, String> {
private Context context;
public ProgressDialog pDialog;
private File pdfFile = new File(Environment
.getExternalStorageDirectory().getPath()
+ "/SAMPLE/"
+ pdfFileName);
public DownloadFileTask(Context context) {
this.context = context;
}
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
if (!pdfFile.exists()) {
pDialog = new ProgressDialog(context);
pDialog.setMessage(getString(R.string.loading));
pDialog.setCancelable(false);
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.show();
}
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... path) {
int count;
if (!pdfFile.exists()) {
if (Utility.isNetworkAvailable(parentActivityContext)) {
try {
String urlLastPath = Utility
.getLastPathFromUrl(path[0]);
String urlEncoded = URLEncoder.encode(urlLastPath,
"utf-8");
String urlDecoded = null;
String urlStr;
if (urlEncoded.contains(" ")) {
urlDecoded = urlEncoded.replaceAll(" ", "%20");
urlStr = SystemInfo.getResourceUrl() + "pdf/"
+ urlDecoded;
} else if (urlEncoded.contains("+")) {
urlDecoded = urlEncoded.replaceAll(
Pattern.quote("+"), "%20");
urlStr = SystemInfo.getResourceUrl() + "pdf/"
+ urlDecoded;
} else {
urlStr = SystemInfo.getResourceUrl() + "pdf/"
+ urlEncoded;
}
URL url = new URL(urlStr);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
// getting file length
int lengthOfFile = urlConnection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(
url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream(Environment
.getExternalStorageDirectory().getPath()
+ "/'SAMPLE/" + pdfFileName);
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) / lengthOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("Error: ", e.getMessage());
}
} else {
openDialog(getString(R.string.error),
getString(R.string.internet_connection_error));
}
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
/** PDF reader code */
Intent intent = new Intent(parentActivityContext,
MuPDFActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(pdfFile));
startActivity(intent);
}
}