اضافه زر لتحميل من رابط للملفات في سكيتشوير

 


الكود المستخدم


android.net.ConnectivityManager connMgr = (android.net.ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {


final String urlDownload = _url;

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlDownload));

final String fileName = URLUtil.guessFileName(urlDownload, null, null);

request.setDescription("URL - " + urlDownload);

request.setTitle(fileName);

request.allowScanningByMediaScanner();

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

request.setDestinationInExternalPublicDir(_path, fileName);

final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

final long downloadId = manager.enqueue(request);

final ProgressDialog prog = new ProgressDialog(this);
prog.setMax(100);
prog.setIndeterminate(true);
prog.setCancelable(false);
prog.setCanceledOnTouchOutside(false);
prog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
prog.setTitle("Downloading...");

new Thread(new Runnable() {
@Override
public void run() {

boolean downloading = true;

while (downloading) {

DownloadManager.Query q = new DownloadManager.Query();

q.setFilterById(downloadId);

android.database.Cursor cursor = manager.query(q);

cursor.moveToFirst();

int bytes_downloaded = cursor.getInt(cursor .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));

int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {

downloading = false;

}

final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

runOnUiThread(new Runnable() {
@Override
public void run() {

prog.setTitle("Downloading...");
prog.setMessage("Downloading the " + fileName + ".\n\nProgress - " + dl_progress + "%");
prog.show();
showMessage("Downloaded");
if (dl_progress == 100) {
prog.dismiss();
}

} });
} } }).start();


} else {
showMessage("No Internet Connection.");
}



إرسال تعليق

اكتب تعليق

أحدث أقدم