Skip to content

Commit

Permalink
fix cached image
Browse files Browse the repository at this point in the history
  • Loading branch information
Balcan committed Jan 29, 2025
1 parent e173a5c commit ec7d98e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
37 changes: 34 additions & 3 deletions commons/src/main/java/org/dhis2/commons/bindings/FileExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,39 @@ fun File.rotateImage(context: Context): File {
else -> bitmap
}

return File(
FileResourceDirectoryHelper.getFileResourceDirectory(context),
val file = File(
FileResourceDirectoryHelper.getFileCacheResourceDirectory(context),
"tempFile.png",
).apply { writeBitmap(bitmap, Bitmap.CompressFormat.JPEG, 100) }
)

file.writeBitmap(bitmap, Bitmap.CompressFormat.JPEG, 100)

return file
}

fun Bitmap.rotateImageAndSave(context: Context): File {
val file = File(
FileResourceDirectoryHelper.getFileCacheResourceDirectory(context),
"tempFile.png",
)

file.writeBitmap(this, Bitmap.CompressFormat.JPEG, 100)

val ei = ExifInterface(file.path)

val orientation =
ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)

val bitmap = when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> rotateImage(this, 90F)
ExifInterface.ORIENTATION_ROTATE_180 -> rotateImage(this, 180F)
ExifInterface.ORIENTATION_ROTATE_270 -> rotateImage(this, 270F)
else -> this
}

file.writeBitmap(bitmap!!, Bitmap.CompressFormat.JPEG, 100)

return file
}

private fun rotateImage(source: Bitmap, angle: Float): Bitmap? {
Expand Down Expand Up @@ -103,9 +132,11 @@ private fun getFilePath(context: Context, uri: Uri): String? {
split[splitIndex].toLong(),
)
}

isExternalStorageDocument(copy) -> {
return Environment.getExternalStorageDirectory().toString() + "/" + split[1]
}

isMediaDocument(copy) -> {
copy = when (split[0]) {
"image" -> MediaStore.Images.Media.EXTERNAL_CONTENT_URI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal fun ImagePickerOptionsDialog(
CarouselButtonData(
onClick = {
onDismiss()
onTakePicture(context)
onSelectFromGallery()
},
enabled = true,
text = stringResource(R.string.from_gallery_v2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.core.content.FileProvider
import org.dhis2.commons.bindings.rotateImage
import org.dhis2.commons.data.FormFileProvider
import org.hisp.dhis.android.core.arch.helpers.FileResourceDirectoryHelper
import java.io.File

private class GetFileResource(
Expand Down Expand Up @@ -58,7 +59,10 @@ fun rememberCameraPicker(
) = with(
LocalContext.current,
) {
val tempFile = File.createTempFile("tempFile", ".png")
val tempFile = File(
FileResourceDirectoryHelper.getFileResourceDirectory(this),
"tempFile.png",
)

val photoUri = FileProvider.getUriForFile(
this,
Expand Down

0 comments on commit ec7d98e

Please sign in to comment.