load img picaso ftp
To load an image using Picasso library in Kotlin from an FTP server, you can follow these steps:
- Add the Picasso dependency to your build.gradle file:
implementation 'com.squareup.picasso:picasso:2.71828'
- Create a function to load the image from the FTP server using Picasso:
fun loadImageFromFtp(url: String, imageView: ImageView) {
val picasso = Picasso.Builder(context)
.downloader(OkHttp3Downloader(createFtpClient()))
.build()
picasso.load(url).into(imageView)
}
private fun createFtpClient(): OkHttpClient {
return OkHttpClient.Builder()
.authenticator { _, response ->
val credential = Credentials.basic("username", "password")
response.request().newBuilder().header("Authorization", credential).build()
}
.build()
}
- Call the function with the FTP image URL and the ImageView where you want to display the image:
val imageUrl = "ftp://example.com/image.jpg"
val imageView = findViewById<ImageView>(R.id.imageView)
loadImageFromFtp(imageUrl, imageView)