๐ค Android Quickstart Guide
This guide helps you integrate the Visionary.ai API into your Android app using Java or Kotlin.
Youโll be able to send images from your device to our API and receive analysis results in real time.
๐ฆ Prerequisitesโ
- Android Studio (Arctic Fox or newer)
- Target SDK โฅ 26
- Internet permission in your
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
๐งฑ Step 1 โ Add Dependenciesโ
Weโll use OkHttp
and Retrofit
to make HTTP requests.
In your build.gradle
(app) file:
dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}
๐ผ๏ธ Step 2 โ Prepare the Image Fileโ
Use this method to get the file from your ImageView
or gallery selection:
val file = File(imageUri.path!!)
val requestFile = file.asRequestBody("image/*".toMediaTypeOrNull())
val imagePart = MultipartBody.Part.createFormData("image", file.name, requestFile)
๐ Step 3 โ Setup the API Clientโ
1. Create the Retrofit Interfaceโ
Define the API endpoint using a Kotlin interface:
interface VisionaryApi {
@Multipart
@POST("https://api.visionary.ai/v1/analyze")
fun analyzeImage(
@Header("Authorization") apiKey: String,
@Part image: MultipartBody.Part
): Call<AnalysisResponse>
}
๐ Explanation
-
@Multipart
indicates a multipart/form-data request (used for file uploads). -
@POST("analyze")
targets the/analyze
endpoint. -
@Header
lets you add theAuthorization
header for your API key. -
@Part
is the image you're sending (prepared earlier).
๐ก AnalysisResponse
should be a data class representing the expected JSON response.
You can mock it if you're just testing for now.
2. Create the Retrofit Interfaceโ
val retrofit = Retrofit.Builder()
.baseUrl("https://api.visionary.ai/v1/")
.addConverterFactory(GsonConverterFactory.create())
.build()
๐ Explanation:
-
baseUrl
defines the root for all API requests. -
GsonConverterFactory
automatically converts JSON responses into Kotlin objects.
3. Create the API Clientโ
val api = retrofit.create(VisionaryApi::class.java)
This creates a usable instance of the VisionaryApi
interface โ your gateway to the API.
4. Make the Request with the Imageโ
val call = api.analyzeImage("Bearer YOUR_API_KEY", imagePart)
โ ๏ธ Don't forget to include the "Bearer "
prefix before your API key.
5. Handle the Responseโ
call.enqueue(object : Callback<AnalysisResponse> {
override fun onResponse(call: Call<AnalysisResponse>, response: Response<AnalysisResponse>) {
if (response.isSuccessful) {
val result = response.body()
// โ
Handle analysis results here
} else {
// โ Handle HTTP errors
}
}
override fun onFailure(call: Call<AnalysisResponse>, t: Throwable) {
// โ ๏ธ Handle network failures or timeouts
}
})
๐งช Testingโ
Use the demo-api-key
to test up to 20 calls/day.
Replace with your real API key when you're ready for production.
๐ง Notesโ
-
Max image size: 5MB
-
Accepted formats: JPEG, PNG, WEBP
-
Avoid uploading screenshots or corrupted files
-
Use
.toRequestBody()
only on valid image types
Need help? โ Check our Integration Troubleshooting guide โ Or visit the FAQ