-2

I am building an application where user can download a wallpaper or save it to desktop directly.

What I want to do now is to add a download counter or number of downloads in a Textview which will display the total number of downloads for that particular image. In other words, number of times that particular image is downloaded by other users.

I am not sure how to do it, I am guessing I can use google analytics, but is there any way to do it without using any library or service ?

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57

2 Answers2

0
  1. Create a static integer variable.
  2. On Every Start of download increase the counter
  3. On every finish of download or error decrease the counter.

Hence the counter will provide you the exact ongoing image download.

Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26
Satyam Anand
  • 377
  • 1
  • 8
0

Its a pretty straight forward task, you have to do some changes on the server side and device side as well.

Changes on server side:
1. Add a db with a table to record the image details like image_url, image_name, image_id, etc. (make sure to have a legit primary key) and then add the most important thing, number of downloads.
2. In your API where you are making the call to server to get the image details, add another parameter of number of downloads.
3. Create another end point to update the successful download.

Changes on device side:
1. Once a download is successful make a call to your new API so that server will update the number of downloads in the Database.
2. Now here is the interesting part, if your download is successful, then in your textview you should increment the download count that you received earlier from the server.
3. If you want the download count to be dynamic, that is, if you want to update the number of downloads every time someone downloads the image, then you can use Firebase Dynamic DB.

Steps to use Firebase Dynamic DB
1. Firebase dynamic DB is free of cost and if you use this you wont have to add any DB to your server as I mentioned in Above steps.
3. Firebase DB is little complicated to implement, so spend appropriate time in understanding it designing a schema for it.
4. Firebase DB has a function that any client with the access can update the values in the DB, and this update will automatically be notified to other clients having a listener on the DB. This function will allow you to get the latest number of downloads without making server call again and again.
5. The most important step, once your download is success, update the Firebase Dynamic DB so that all the clients get notified with the latest value which you can use to update the TextView

I suggest you to use Firebase DB since it is fast and you wont have to do additional work on server, and the most important part, in future if you want to add Push Notifications to your app, you can use the same Firebase Project to add push notifications too.

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57