Widget - Webview Asyn Task

webview1.setDownloadListener(new DownloadListener() {

public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,                 long contentLength) {
FileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
new DownloadFile().execute(Uri.parse(url).toString());
}});



}
private ProgressDialog mProgressDialog;
private java.io.File PathDir = Environment.getExternalStorageDirectory();
private String PathName = "Downloads_File";
private String FileName = "";
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
checkAndCreateDirectory(PathName);
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle(FileName);
mProgressDialog.setMessage("Downloading " + FileName + ". Please wait...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... Url) {
try {
java.net.URL url = new java.net.URL(Url[0]);
java.net.URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
//String filepath = Environment.getExternalStorageDirectory().getPath();
java.io.InputStream input = new java.io.BufferedInputStream(url.openStream());
java.io.OutputStream output = new java.io.FileOutputStream(PathDir + "/" + PathName + "/" + FileName);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
showMessage(e.getMessage().toString());
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
showMessage("Download Complite");
}
}
public void checkAndCreateDirectory(String PathName){
java.io.File new_dir = new java.io.File(PathDir + "/" + PathName);
if (!new_dir.exists()){
new_dir.mkdirs();
}

Komentar

Cara pembuatan Apps paling Populer

Create Stopwatch App in Android using Sketchware

How to enable download in webview in Sketchware apps?

TextInputLayout in Sketchware

Create Stopwatch App in Android using Sketchware

How to integrate Admob Ads in Sketchware project using AIDE?

How to find and​ highlight a word in a text field in Sketchware?

Code for implementing Notifications in Sketchware

A Flash Light App in Sketchware

Create a Stopwatch App using Chronometer in Sketchware

Create app to save all passwords, protected using voice key