How to load and caching image without libraries | Android | Kotlin
At first, this project asked me as a test when I applied for a job in a company.
You can use a library like Picasso and Glide.
Let’s Start …
The first step is to add internet permission in manifest file:
Then we need to add coroutines dependency
We have to make CachingUtil class that contains 3 functions:
The first function is getBitmapFromURL which downloads the image from the URL.
The second function is storeBitmap which will cache the image as a file and store it in the internal storage.
The last one is loadBitmap that’s will get the caching image from internal storage.
In our case, we don’t need permission to access the storage.
The last step is the MainActivity
The first step is to check if we have a caching image to display it.
Then we should initialize the button click listener As follows:
_Should check if there is an active job, the cause is
the process of downloading the image takes some time,so we avoid wasting resources.
_When you click the button, a new image will be fetched, here I use async and await, so it will wait and when the result is fetched, the next stage is to store the image and then switch to the main thread and display the image.
Here we are done
Sorry for the short explanation
Here you can find the complete project