@@ -3,8 +3,14 @@ package com.margelo.nitro.multipleimagepicker
3
3
import android.app.Activity
4
4
import android.content.Context
5
5
import android.content.Intent
6
+ import android.graphics.Bitmap
7
+ import android.graphics.BitmapFactory
6
8
import android.graphics.Color
9
+ import android.graphics.Matrix
7
10
import android.net.Uri
11
+ import android.util.Log
12
+ import androidx.exifinterface.media.ExifInterface
13
+ import java.io.IOException
8
14
import androidx.core.content.ContextCompat
9
15
import com.facebook.react.bridge.BaseActivityEventListener
10
16
import com.facebook.react.bridge.ColorPropConverter
@@ -163,7 +169,13 @@ class MultipleImagePickerImp(reactContext: ReactApplicationContext?) :
163
169
localMedia.forEach { item ->
164
170
if (item != null ) {
165
171
val media = getResult(item)
166
- data + = media // Add the media to the data array
172
+ // Adjust orientation using ExifInterface for Android (only for images)
173
+ val adjustedMedia = if (media.type == ResultType .IMAGE ) {
174
+ adjustOrientation(media, item.path)
175
+ } else {
176
+ media
177
+ }
178
+ data + = adjustedMedia // Add the media to the data array
167
179
}
168
180
}
169
181
resolved(data)
@@ -634,6 +646,7 @@ class MultipleImagePickerImp(reactContext: ReactApplicationContext?) :
634
646
parentFolderName = item.parentFolderName,
635
647
creationDate = item.dateAddedTime.toDouble(),
636
648
crop = item.isCut,
649
+ orientation = null , // Will be populated by adjustOrientation for images
637
650
path,
638
651
type,
639
652
fileName = item.fileName,
@@ -644,6 +657,55 @@ class MultipleImagePickerImp(reactContext: ReactApplicationContext?) :
644
657
return media
645
658
}
646
659
660
+ private fun adjustOrientation (pickerResult : PickerResult , filePath : String ): PickerResult {
661
+ try {
662
+ val ei = ExifInterface (filePath)
663
+ val orientation = ei.getAttributeInt(
664
+ ExifInterface .TAG_ORIENTATION ,
665
+ ExifInterface .ORIENTATION_UNDEFINED
666
+ )
667
+
668
+ val rotatedBitmap: Bitmap ? = when (orientation) {
669
+ ExifInterface .ORIENTATION_ROTATE_90 -> rotateImage(filePath, 90f )
670
+ ExifInterface .ORIENTATION_ROTATE_180 -> rotateImage(filePath, 180f )
671
+ ExifInterface .ORIENTATION_ROTATE_270 -> rotateImage(filePath, 270f )
672
+ else -> BitmapFactory .decodeFile(filePath)
673
+ }
674
+
675
+ rotatedBitmap?.let { bitmap ->
676
+ val width = bitmap.width.toDouble()
677
+ val height = bitmap.height.toDouble()
678
+ // Update width, height, and orientation in pickerResult
679
+ return pickerResult.copy(
680
+ width = width,
681
+ height = height,
682
+ orientation = orientation.toDouble()
683
+ )
684
+ }
685
+ } catch (e: IOException ) {
686
+ Log .e(TAG , " Failed to adjust orientation" , e)
687
+ }
688
+ // Return with orientation info even if bitmap processing fails
689
+ return try {
690
+ val ei = ExifInterface (filePath)
691
+ val orientation = ei.getAttributeInt(
692
+ ExifInterface .TAG_ORIENTATION ,
693
+ ExifInterface .ORIENTATION_UNDEFINED
694
+ )
695
+ pickerResult.copy(orientation = orientation.toDouble())
696
+ } catch (e: IOException ) {
697
+ Log .e(TAG , " Failed to read orientation" , e)
698
+ pickerResult
699
+ }
700
+ }
701
+
702
+ private fun rotateImage (filePath : String , degree : Float ): Bitmap ? {
703
+ val bitmap = BitmapFactory .decodeFile(filePath)
704
+ val matrix = Matrix ()
705
+ matrix.postRotate(degree)
706
+ return Bitmap .createBitmap(bitmap, 0 , 0 , bitmap.width, bitmap.height, matrix, true )
707
+ }
708
+
647
709
override fun getAppContext (): Context {
648
710
return reactApplicationContext
649
711
}
0 commit comments