2

Well, I'm working on an app that loads images from Internet. Everything is OK so far, but I'd like to know how could I calculate how long such images take to be loaded from Internet?

There is any method on Bitmap to make that? Maybe there is any other way that you would suggest me?

Cheers,

Javanes
  • 91
  • 2
  • 10
  • http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog/3028660#3028660 – Lalit Poptani Jan 23 '12 at 10:09
  • You may want to look at `RemoteImageView` which is part of the `Ignition` package that includes tools and basic framework for Android applications. The code is hosted at : https://github.com/kaeppler/ignition `RemoteImageView` uses a `ViewSwitcher` to alternate between a loading image and the actual loaded image. If you do decide to use the time calculation, it may be easier for you to hack the package rather than write you own. – Saad Farooq Jan 23 '12 at 10:02

2 Answers2

1

Well if you know the filesize b of the image (in bytes), and the speed s at which it's downloading (in bytes per second), then the time t (in seconds) to download the file will be:

t = b / s

Simple math really to convert your units as needed. Don't forget that this value is constantly changing as the download speed changes.

Edit: Now if you're only looking to calculate how long the image took to download and maybe display this information after the fact, then a simple solution would be to start a timer when the download is initiated and stop it when it's done.

Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
  • Well, thanks. But that's the point, that will depend on many variables. I'd like to know if there is something simpler. For instance: The image is loaded. It's true or false. And how long take between those two stages. – Javanes Jan 22 '12 at 18:52
  • @Javanes If you really want to display the _time remaining_ to load an image from the Internet, you have no choice. – Marvin Pinto Jan 22 '12 at 18:54
0

HTTP response will contain a Content-Length field this will let you know how much is to be downloaded. If you know the speed of your conection then you can work out the estimated time using time = dataSize / downloadSpeed.

You can also use the knowlege of how much you have downloaded so far to work out how long it will take using linear extrapolation. time = (dataSize * (timeNow - timeStart)) / dataDownloadedNow

Gareth A. Lloyd
  • 1,774
  • 1
  • 16
  • 26
  • Hi Gareth, yes, I got. By they way, I'm using the time to control a sleep on Thread that dismiss a ProgressBar. Do you have any suggestion how I could control the sleep or maybe how could I control the Thread without time? – Javanes Jan 23 '12 at 02:16