diff --git a/app/src/main/java/com/google/android/samples/socialite/repository/ChatRepository.kt b/app/src/main/java/com/google/android/samples/socialite/repository/ChatRepository.kt index 314f5b1d..f74bc1d0 100644 --- a/app/src/main/java/com/google/android/samples/socialite/repository/ChatRepository.kt +++ b/app/src/main/java/com/google/android/samples/socialite/repository/ChatRepository.kt @@ -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 @@ -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 @@ -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(