قائمة المنشورات


 دروس sketchesware


 العودة إلى المنتدى

موح درويد   

الدرس 107: اضافة تحميل الملفات داخل تطبيقك على sketchware

الهكر الاسود | منشور الثلاثاء يوليو 10 2018, 19:14 | 3 مشاهدة

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 = "موح درويد";
private String FileName = "";
private class DownloadFile extends AsyncTask{
@Override
protected void onPreExecute() {
super.onPreExecute();
checkAndCreateDirectory(PathName);
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle(FileName);
mProgressDialog.setMessage("Downloading " + FileName + ". انتظر قليلا...");
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();
}
نبذة عن الكاتب