Kotlin Android Integration
Use Kotlin and OkHttp to upload images and remove backgrounds directly inside Android applications using AI-powered processing.
Add OkHttp Dependency
Add the OkHttp library inside your Gradle dependencies before making API requests.
implementation(
"com.squareup.okhttp3:okhttp:4.12.0"
)
Import Required Classes
Import OkHttp and File classes before creating multipart upload requests.
import okhttp3.*
import java.io.File
Kotlin Upload Example
Upload images and receive transparent PNG results using Kotlin and OkHttp multipart requests.
val client = OkHttpClient()
val file =
File("/storage/image.jpg")
val requestBody =
MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"file",
file.name,
file.asRequestBody(
"image/jpeg"
.toMediaType()
)
)
.build()
val request = Request.Builder()
.url(
"https://api.bybybg.com/v1/remove-bg"
)
.addHeader(
"X-API-Key",
"YOUR_API_KEY"
)
.post(requestBody)
.build()
client.newCall(request)
.enqueue(object : Callback {
override fun onFailure(
call: Call,
e: IOException
) {
println("Failed")
}
override fun onResponse(
call: Call,
response: Response
) {
if(response.isSuccessful){
println(
"Background removed"
)
}else{
println(
"Error: ${response.code}"
)
}
}
})
Recommended Android Features
Camera Support
Capture images directly using Android camera APIs.
Gallery Picker
Allow users to select images from their device gallery.
Progress Loader
Display animated loading indicators during AI image processing.
Save PNG
Store transparent PNG images inside local device storage.
Recommended Practices
- Compress images before uploading.
- Validate supported image formats.
- Handle connection failures and retries.
- Never expose private API keys publicly.
- Use backend APIs in production environments.
Common API Errors
Continue to Error Handling
Learn about API limits, status codes, troubleshooting, and error handling best practices.