Skip to main content

๐Ÿค– 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 the Authorization 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