How to download a file in Kotlin
We often need to download files in our Android application. The most basic way of doing this will be opening a URL connection and using InputStream
to read the content of the file and storing it in a local file using FileOutputStream
; all this is in a background thread using AsyncTask
. However, we don’t want to reinvent the wheel. There are a lot of libraries out there that handle all this stuff very nicely for us and make our work super easy, helping us create clean code.
We can use Volley (https://developer.android.com/training/volley/index.html), a networking library by developers at Google, which makes network communication very easy and fast. Another one we can use is OkHttp (by Square), which is very efficient, and we can use it along with Retrofit (for HTTP API).
For this recipe, we will be using a networking library called Fuel, which is written in Kotlin.
Getting ready
Create a new Android project and add an activity. Now, add fuel dependencies to your...