Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In
Continue with Google
or use


Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here
Continue with Google
or use


Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the question so it can be answered easily.

Please choose the appropriate section so the question can be searched easily.

Please choose suitable Keywords Ex: question, poll.

Browse
Type the description thoroughly and in details.

Choose from here the video type.

Put Video ID here: https://www.youtube.com/watch?v=sdUUx5FdySs Ex: "sdUUx5FdySs".


Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

AiSuperSmart Qna

AiSuperSmart Qna Logo AiSuperSmart Qna Logo

AiSuperSmart Qna Navigation

  • Home
  • Trending
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Trending
  • Blog
  • Contact Us
Home/ Questions/Q 2821
Next
Answered
Hiba Zyati
  • 1
Hiba ZyatiYe Yoooo!
Asked: 1 year ago2024-11-21T15:07:33+00:00 2024-11-21T15:07:33+00:00In: Coding

Using Google's generative AI for images in Kotlin

  • 1

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.

coding
  • 1 1 Answer
  • 77 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    Aisupersmart God Level!
    2024-11-21T15:08:24+00:00Added an answer about 1 year ago

    The error you’re encountering is because the Content class 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 Content class seems to be a custom class or one from a different library. The official Google AI Client SDK doesn’t have a built-in Content class for representing different input types like images.

    The Solution:

    There are two ways to address this:

    1. Using InputImage:

    The Google AI Client SDK offers an InputImage class specifically designed for passing images to the generateContent function. Here’s the corrected code:

    Kotlin
    lifecycleScope.launch {
        val prompt = arrayOf(
            InputImage.fromBitmap(selectedImageBitmap),
            "What is in this photo?"
        )
        val response = withContext(Dispatchers.IO) {
            generativeModel.generateContent(prompt)
        }
        withContext(Dispatchers.Main) {
            OutputValue = response.text
            myTextOutput.text = OutputValue
        }
    }
    

    This code creates an InputImage object directly from your selectedImageBitmap and includes it in the prompt array.

    1. Using ByteString (For more advanced users):

    If you want more control over the image data, you can convert your Bitmap to a ByteString before passing it to the prompt. Here’s an example:

    Kotlin
    val imageByteArray = selectedImageBitmap.toBytes("PNG") // Convert to byte array
    val imageByteString = ByteString.copyFrom(imageByteArray)
    val prompt = arrayOf(
        imageByteString,  // Raw image data
        "What is in this photo?"
    )
    // Rest of the code remains the same
    

    Explanation:

    • InputImage.fromBitmap(selectedImageBitmap): This creates an InputImage object 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:

    • Make sure you have the correct version of the Google AI Client SDK dependency in your build.gradle.kts file.

    Remember, these are just two options. Choose the one that best suits your needs and coding style.

    • 1
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Related Questions

  • How do 20 questions AI algorithms work?
  • How to dynamically add copy text functionality in Vue.js in Deep chat library?
  • Best way to automate testing of AI algorithms?
  • How to Build an AI Chatbot that can do CRUD Operations via API Requests?
  • Best AI to Learn Programming?

Sidebar

Ask A Question

Stats

  • Questions 115
  • Answers 371
  • Best Answers 49
  • Users 3k
  • Popular
  • Answers
  • gaming motar

    Looksmax AI Free Alternatives or Similar Ai?

    • 167 Answers
  • Harshvardhan Kadam

    Viggle Ai free Alternatives or Similar Ai?

    • 21 Answers
  • gaming motar

    where can i get more characters bots for silly tavern?

    • 5 Answers
  • ConsistentCharacterAI
    ConsistentCharacterAI added an answer Hey, great question! Finding a good free AI hairstyle changer… January 31, 2026 at 12:06 pm
  • SCSC
    SCSC added an answer SC娛樂城是許多玩家關注,現在線上娛樂城有許多遊戲可以選擇,再加上現在優惠活動非常多,所以說短時間就吸引不少玩家註冊,究竟各位在挑選娛樂城該注意些什麼呢?今天這篇文章帶各位深入了解!   ►運彩投注 現在有越來越多民眾選擇再線上娛樂場投注,現金版是什麼?太陽城就提供玩家賺錢機會,地下運彩與台灣運彩的差異處再賠率以及玩法,在博弈娛樂城可以享有業界最高賠率,而且每場比賽都有提供單場給下注,這對於玩家來說勝率可以大大提升,如果還沒有在娛樂城體驗過的一定要試試看!現在有許多最新娛樂城在等著各位。 運彩玩法作為博彩投資的項目之一,鮮少會有人認真看待策略,於對購買運彩的人來說,一部分網友只是想要參與賽事增加觀賞比賽轉播的娛樂性與刺激,所以在賽事結果的輸贏方面,看法多少都是較隨意的。 October 22, 2025 at 9:48 am
  • SCSC
    SCSC added an answer SC娛樂城是許多玩家關注,現在線上娛樂城有許多遊戲可以選擇,再加上現在優惠活動非常多,所以說短時間就吸引不少玩家註冊,究竟各位在挑選娛樂城該注意些什麼呢?今天這篇文章帶各位深入了解!   ►運彩投注 現在有越來越多民眾選擇再線上娛樂場投注,現金版是什麼?太陽城就提供玩家賺錢機會,地下運彩與台灣運彩的差異處再賠率以及玩法,在博弈娛樂城可以享有業界最高賠率,而且每場比賽都有提供單場給下注,這對於玩家來說勝率可以大大提升,如果還沒有在娛樂城體驗過的一定要試試看!現在有許多最新娛樂城在等著各位。 運彩玩法作為博彩投資的項目之一,鮮少會有人認真看待策略,於對購買運彩的人來說,一部分網友只是想要參與賽事增加觀賞比賽轉播的娛樂性與刺激,所以在賽事結果的輸贏方面,看法多少都是較隨意的。 October 22, 2025 at 9:48 am

Related Questions

  • Kubeflow vs Vertex AI Pipelines

    • 1 Answer
  • How do 20 questions AI algorithms work?

    • 1 Answer
  • How to dynamically add copy text functionality in Vue.js in ...

    • 1 Answer
  • Best way to automate testing of AI algorithms?

    • 1 Answer
  • How to Build an AI Chatbot that can do CRUD ...

    • 1 Answer

Top Members

Aisupersmart

Aisupersmart

  • 0 Questions
  • 1k Points
God Level!
Hiba Zyati

Hiba Zyati

  • 50 Questions
  • 186 Points
Ye Yoooo!
gaming motar

gaming motar

  • 29 Questions
  • 101 Points
Haa haa ha!

Trending Tags

ai ai death calculator aithor algosone character ai code coding domoai domo ai gemini health huggingface krutrim looksmax ai notta oasis promeai revoicer sora tavern

Explore

  • Home
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Groups
  • Ai Tools
  • Ai Updates From Social Media
  • Communities
  • Polls
  • Tags
  • Badges
  • Users

Footer

AiSuperSmart Qna

AiSuperSmart

AiSuperSmart is an Ai tool Directory And Also Provide Updates Related Ai World. Try Our Ai tools for Free

Legal Pages

  • About us
  • Contact Us
  • Privacy policy
  • Terms & condition

Tools

  • Free Image Background Remover HD
  • Image Converter Ai

Help

  • Knowledge Base
  • Support

Follow

© 2024 AiSuperSmart. All Rights Reserved
With Love by Hsk Team

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.