Skip to content

Commit 5d16dad

Browse files
committed
avoid decoding image file multiple times in validateImageIsFullPerson()
1 parent 182a0f9 commit 5d16dad

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

data/src/main/java/com/android/developers/androidify/data/ImageGenerationRepository.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,16 @@ internal class ImageGenerationRepositoryImpl @Inject constructor(
5656
firebaseAiDataSource.validatePromptHasEnoughInformation(inputPrompt)
5757

5858
private suspend fun validateImageIsFullPerson(file: File): ValidatedImage {
59+
val bitmap = BitmapFactory.decodeFile(file.absolutePath)
5960
val validateImageResult = if (remoteConfigDataSource.useGeminiNano()) {
60-
geminiNanoDataSource.validateImageHasEnoughInformation(BitmapFactory.decodeFile(file.absolutePath))
61+
geminiNanoDataSource.validateImageHasEnoughInformation(bitmap)
6162
} else {
6263
null
6364
}
6465

6566
// If validating image with Nano is not successful, fallback to using Firebase AI
6667
return validateImageResult
67-
?: firebaseAiDataSource.validateImageHasEnoughInformation(
68-
BitmapFactory.decodeFile(
69-
file.absolutePath,
70-
),
71-
)
68+
?: firebaseAiDataSource.validateImageHasEnoughInformation(bitmap)
7269
}
7370

7471
@Throws(InsufficientInformationException::class)
@@ -138,7 +135,8 @@ internal class ImageGenerationRepositoryImpl @Inject constructor(
138135
}
139136

140137
override suspend fun saveImageToExternalStorage(imageBitmap: Bitmap): Uri {
141-
val cacheFile = localFileProvider.createCacheFile("androidify_image_result_${UUID.randomUUID()}.png")
138+
val cacheFile =
139+
localFileProvider.createCacheFile("androidify_image_result_${UUID.randomUUID()}.png")
142140
localFileProvider.saveBitmapToFile(imageBitmap, cacheFile)
143141
return localFileProvider.saveToSharedStorage(cacheFile, cacheFile.name, "image/png")
144142
}

0 commit comments

Comments
 (0)