I’m trying to use Google’s generative AI in Kotlin in Android Studio, to write an app that gives me information about a photo. So I would like to pass an image and a text to the gemini-1.5-flash API, but I am not succeeding. This is my code:
lifecycleScope.launch {
val prompt = arrayOf(
Content.Image(selectedImageBitmap),
"What is in this photo?")
val response = withContext(Dispatchers.IO) {
generativeModel.generateContent(prompt)
}
withContext(Dispatchers.Main) {
OutputValue = response.text
myTextOutput.text = OutputValue
}
}
selectedImageBitmap is a bitmap object.
In build.grandle.kts file, I’ve added the dependencies:
implementation("com.google.ai.client.generativeai:generativeai:0.9.0")
And in my MainActivity, I’ve added the import:
import com.google.ai.client.generativeai.GenerativeModel
I have the following error: Unresolved reference: Content.
That means, the compiler cannot find the definition of ‘Content.
What’s wrong with my code? What do I need to do to get it to work?
I expected generativeModel.generateContent() to easily accept text, images, PDFs, and other input types.
The error you’re encountering is because the
Contentclass you’re using isn’t part of the official Google AI Client SDK for Kotlin. Here’s how to fix it and use Google’s generative AI for image analysis in your Kotlin code:The Problem:
The
Contentclass seems to be a custom class or one from a different library. The official Google AI Client SDK doesn’t have a built-inContentclass for representing different input types like images.The Solution:
There are two ways to address this:
InputImage:The Google AI Client SDK offers an
InputImageclass specifically designed for passing images to thegenerateContentfunction. Here’s the corrected code:This code creates an
InputImageobject directly from yourselectedImageBitmapand includes it in the prompt array.ByteString(For more advanced users):If you want more control over the image data, you can convert your
Bitmapto aByteStringbefore passing it to the prompt. Here’s an example:Explanation:
InputImage.fromBitmap(selectedImageBitmap): This creates anInputImageobject specifically designed for the Google AI Client SDK, ensuring compatibility.ByteString: This class represents raw byte data, allowing you to pass the image data directly if you prefer.Additional Notes:
build.gradle.ktsfile.Remember, these are just two options. Choose the one that best suits your needs and coding style.