In this example, I will show you how to download image using url and display download image into imageView.
Kotlin Download Image Using Url With Retrofit Example Video
- You can also go through this video explanation.
Kotlin Download Image Using Url With Retrofit Example
Creating a New Project
- So first create a new Android Studio project using Kotlin.
- Once the project is loaded come inside the activity_main.xml and remove the Hello World TextView.
Android studio project screen
Step 1:
Open build.gradle and add following dependency and rebuild the project.
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
Step 2:Open AndroidManifest.xml and add Internet Permission.<uses-permission android:name="android.permission.INTERNET"/>Step 3:Create new Class:- DataService.ktDataService.ktpackage com.kotlinkatta.imgdowretrofit
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Url
public interface DataService {
@GET
fun downloadFileUsingUrl(@Url fileUrl: String): Call<ResponseBody>
}Step 4:Create new Class:- RetrofitClient.ktRetrofitClient.ktpackage com.kotlinkatta.imgdowretrofit
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
object RetrofitClient {
var BASE_URL:String="https://wallpapersite.com"
val getClient: DataService
get() {
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.build()
return retrofit.create(DataService::class.java)
}
}Step 5:MainActivity.ktAdd following code into MainActivity.kt.package com.kotlinkatta.imgdowretrofit
import android.app.ProgressDialog
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class MainActivity : AppCompatActivity() {
lateinit var img_display_image : ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
img_display_image = findViewById<ImageView>(R.id.img_display_image)
funDownloadImage()
}
private fun funDownloadImage() {
@Suppress("DEPRECATION")
val progressDialog = ProgressDialog(this@MainActivity)
progressDialog.setTitle("Image is downloading, please wait")
progressDialog.show()
val call: Call<ResponseBody> = RetrofitClient.getClient.downloadFileUsingUrl("/images/pages/pic_w/6408.jpg")
call.enqueue(object : Callback<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
val bytes: ByteArray = response.body()!!.bytes()
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
img_display_image.setImageBitmap(bitmap)
progressDialog.dismiss()
}
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
progressDialog.dismiss()
}
})
}
}Step 6:activity_main.xmlAdd following code into activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img_display_image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>OutPut:
Comments
Post a Comment