Webservice.java
public static File createFolders(Context context, String type) {
String folderpath = db_name + "/";
folderpath = folderpath + "Diary/Database";
Boolean isSDPresent = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
File myNewFolder = null;
if (isSDPresent) {
String newFolder = "/" + folderpath;
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
myNewFolder = new File(extStorageDirectory + newFolder);
if (myNewFolder.exists()) {
} else {
myNewFolder.mkdirs();
}
} else {
String newFolder = folderpath;
myNewFolder = context.getDir(newFolder, Context.MODE_PRIVATE);
if (!myNewFolder.exists()) {
myNewFolder.mkdirs();
}
}
return myNewFolder;
}
String filename = data.get(position);
String[] parts = filename.split("/");
String part2 = parts[parts.length - 1];
if (checkFile(part2, "d")) {
co = 1;
File newFolder = Webservice.createFolders(context, "d");
openDocument(newFolder + "/" + part2);
} else {
co = 1;
new DownloadFileFromURL(position).execute(data.get(position));
}
private boolean checkFile(String path, String type) {
File newFolder;
if (type.equals("d")) {
newFolder = Webservice.createFolders(context, "d");
} else {
newFolder = Webservice.createFolders(context, "i");
}
File file = new File(newFolder + "/" + path);
if (file.exists())
return true;
else
return false;
}
private class DownloadFileFromURL extends AsyncTask<String, String, String> {
private ProgressDialog p;
private int pos = 0;
public DownloadFileFromURL(int p) {
pos = p;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
p = new ProgressDialog(context);
p.setMessage("Downloading 0%");
p.setIndeterminate(false);
p.setProgressStyle(ProgressDialog.STYLE_SPINNER);
p.setCancelable(false);
p.show();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
p.setMessage("Downloading " + values[0] + "%");
}
@Override
protected String doInBackground(String... f_url) {
int count;
String filename = f_url[0];
String[] parts = filename.split("/");
String part2 = parts[parts.length - 1];
File myNewFolder = null;
if (part2.endsWith(".JPG")
|| part2.endsWith(".jpg")) {
myNewFolder = Webservice.createFolders(context, "i");
} else if (part2.endsWith(".png")
|| part2.endsWith(".PNG")) {
myNewFolder = Webservice.createFolders(context, "i");
} else if (part2.endsWith(".GIF")
|| part2.endsWith(".gif")) {
myNewFolder = Webservice.createFolders(context, "i");
} else {
myNewFolder = Webservice.createFolders(context, "d");
}
try {
System.out.println("Downloading");
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
OutputStream output = new FileOutputStream(myNewFolder + "/" + part2);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
//if (!Webservice.isCancelled) {
total += count;
// writing data to file
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
/* } else {
break;
}*/
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
//Log.e("Error: ", e.getMessage());
}
return myNewFolder + "/" + part2;
}
/**
* After completing background task
**/
@Override
protected void onPostExecute(String s) {
p.dismiss();
}
}