Skip to content

Commit

Permalink
Gemini multimodal in chat
Browse files Browse the repository at this point in the history
Add image support in chat
  • Loading branch information
tezan committed Jun 26, 2024
1 parent 3e55d4f commit 4c482c0
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package com.google.android.samples.socialite.repository

import android.content.Context
import android.widget.Toast
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
Expand All @@ -36,6 +39,7 @@ import com.google.android.samples.socialite.model.ChatDetail
import com.google.android.samples.socialite.model.Message
import com.google.android.samples.socialite.widget.model.WidgetModelRepository
import dagger.hilt.android.qualifiers.ApplicationContext
import java.io.InputStream
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -113,8 +117,17 @@ class ChatRepository @Inject internal constructor(
)

// Send a message prompt to the model to generate a response
var response = try {
chat.sendMessage(text).text?.trim() ?: "..."
var response = try {
if (mediaMimeType?.contains("image") == true) {
val inputStream: InputStream? = appContext.contentResolver.openInputStream(
Uri.parse(mediaUri))
val bitmap: Bitmap = BitmapFactory.decodeStream(inputStream)
inputStream?.close()

chat.sendMessage(bitmap).text?.trim() ?: "..."
} else {
chat.sendMessage(text).text?.trim() ?: "..."
}
} catch (e: Exception) {
e.printStackTrace()
appContext.getString(
Expand Down

0 comments on commit 4c482c0

Please sign in to comment.