How to measure the download speed using bytes_downloaded and bytes_total Android / Java


How can I calculate the file download speed using two values 1.Total file size and 2.Total Bytes being downloaded.

@Override public void onClick(View v) { // TODO Auto-generated method stub if (v.getId() == R.id.start_ed) { DownloadManager.Request request = new DownloadManager.Request( Uri.parse(durl)); request.setTitle("File Download"); request.setDescription("file is being download"); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); String nof = URLUtil.guessFileName(durl, null, MimeTypeMap.getFileExtensionFromUrl(durl)); request.setDestinationInExternalPublicDir( Environment.DIRECTORY_DOWNLOADS, nof); final DownloadManager downloadManager = (DownloadManager) MainActivity.this .getSystemService(Context.DOWNLOAD_SERVICE); // long id = downloadManager.enqueue(request); //This is to get the // id of the download. final long download_id = downloadManager.enqueue(request); final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1); final TextView current_tvm = (TextView) findViewById(R.id.current_tv); final TextView totl_tv = (TextView) findViewById(R.id.totalsize); new Thread(new Runnable() { @Override public void run() { boolean downloading = true; while (downloading) { DownloadManager.Query q = new DownloadManager.Query(); q.setFilterById(download_id); Cursor cursor = downloadManager.query(q); cursor.moveToFirst(); final int bytes_downloaded = cursor.getInt(cursor .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); final 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() { // totl_tv.setText(bytes_total); // current_tvm.setText(bytes_downloaded); String i = android.text.format.Formatter .formatFileSize(MainActivity.this, bytes_downloaded); System.out.println(i); current_tvm.setText(i); mProgressBar.setProgress((int) dl_progress); } }); // Log.d(Constants.MAIN_VIEW_ACTIVITY, // statusMessage(cursor)); cursor.close(); } } }).start(); } }

So now I have two values bytes_downloaded and bytes_total of a file, how can I calculate file download speed using this two values. Thanks in Advance.


Since you already have the bytes_downloaded value, all you need now is the time dimension.

bytes_downloaded / seconds taken = speed in bytes per second

If it took 10 seconds to download 10000 bytes, your download speed will be

10000 / 10 = 1000 bps = 8 kbps

The total file size is not required to calculate the speed, however, it is required to calculate the estimated time to completion.


You need to do sampling i.e. have a static variable which will have total number of bytes downloaded. For sampling you decided to go with sec i.e. the value of bytes downloaded will be checked after 1000ms. Now you need to run an asychronous task(which will check your bytes downloaded and perform your sampling work within a loop with a pre-decided interval of 1000ms) and it should keep updating(using publish API) your UI with the speed calculated.


  • WebView not loading the file from internal storage
  • Android DownloadManager save to Download folder
  • How to record audio via bluetooth mic
  • How to add Job Description into Jenkins email notification content
  • Accessing environment variables set using mod_rewrite in PHP
  • Should I use static function in c# where many calls the same func?
  • CRATE and elasticsearch-cloud-aws plugin compatibility
  • How do I migrate Facebook Comments from one site to another?
  • What is the variable scope in Meteor client side?
  • SQL Service Broker: Collecting data from distributed sources.
  • Cannot auto configure pydev in eclipse
  • Visual VM showing strange behavior
  • What's included in the “Initialization time” in the Symfony2 web profiler?
  • Programmatically getting SQL Cluster Virtual Name
  • Sinatra on Passenger always fails on first attempt
  • Transaction coordinator for wcf distributed transactions
  • How to get database credentials into a c# application without committing it to source code?
  • How do I debug a segmentation fault in Mono on Ubuntu without any debugger?
  • How do I unit test for machine specific behaviour?
  • Greek letters in a GUI - PYTHON
  • Do commands run from current directory in a shell script?
  • My PHP code is commented out
  • How do I clear the screen in C? [duplicate]
  • Error while installing ggplot in python
  • Configure WAF Application Gateway in front of App Services [closed]
  • yarn hadoop 2.4.0: info message: ipc.Client Retrying connect to server
  • How to execute Blackberry OS 6 app in Blackberry 10 OS
  • How to create MSBuild targets file to deploy files?
  • including Python.h in C++ file CDT
  • Representing intervals or ranges? [closed]
  • Visual Basic 6 on Win7 64bit: Will developed program run on 32-bit Win OS?
  • Read a file in “chunks” using PHP
  • How could I prohibit anonymous access to my NodeRed UI Dashboard on IBM Cloud(Bluemix)?
  • Hiding Directories Programatically in C#
  • Loading fixtures in sails tests
  • What is the use of a session store?
  • How to skip require in ruby?
  • Saving image to sd with current date and time in name doesn't work
  • Yii2: Config params vs. const/define
  • 0x202A in filename: Why?