From e79ca7f3fc2c1968d58bc50d29d0991fbeee2734 Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Wed, 2 Dec 2026 15:04:20 +0100 Subject: [PATCH 1/2] fix(android): read Exif orientation from the image data, not the file path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `getOrientation` resolved the source to a `File` first, which only works for `file:` URIs and for `content:` URIs that expose `MediaStore.DATA` (usually not the case under scoped storage). For `https:` and `data:` sources it returned 0, so the crop coordinates were never remapped — while `BitmapRegionDecoder` crops raw, un-rotated pixels. A portrait photo taken with `Orientation=Rotate90` and served over HTTPS cropped the wrong part of the image entirely, and the crop was transposed. iOS and web both handle this correctly, so Android was the odd one out for every remote rotated image. Read the tag from a stream over the source instead, which works for every URI scheme the module accepts. Sources without Exif data stay upright rather than failing the crop. --- .../imageeditor/ImageEditorModuleImpl.kt | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt index 53a92f3..90322f4 100644 --- a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt +++ b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt @@ -35,6 +35,7 @@ import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.ReadableType import com.facebook.react.bridge.WritableMap import com.facebook.react.common.ReactConstants +import java.io.BufferedInputStream import java.io.ByteArrayInputStream import java.io.File import java.io.FileInputStream @@ -223,7 +224,7 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { val imageHeight: Int = decoder.height val imageWidth: Int = decoder.width - val orientation = getOrientation(reactContext, Uri.parse(uri)) + val orientation = getOrientation(Uri.parse(uri), headers) val (left, top) = when (orientation) { @@ -285,7 +286,7 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { @Suppress("DEPRECATION") BitmapRegionDecoder.newInstance(it, false) } ?: throw Error("Could not create bitmap decoder. Uri: $uri") - val orientation = getOrientation(reactContext, Uri.parse(uri)) + val orientation = getOrientation(Uri.parse(uri), headers) val (x, y) = when (orientation) { 90 -> yPos to decoder.height - rectWidth - xPos @@ -350,6 +351,31 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { } } + /** + * The rotation in degrees the source image has to be rotated clockwise by to be displayed + * upright, or 0 when it carries no such information. Read from the image data itself, so it + * works for remote and `data:` URIs as well as files. + */ + private fun getOrientation(uri: Uri, headers: HashMap?): Int { + val exif = + try { + openBitmapInputStream(uri.toString(), headers) + ?.let { input -> input.use { ExifInterface(BufferedInputStream(it)) } } + } catch (e: IOException) { + // Images without Exif data are not decodable as Exif: treat them as upright + // rather than failing the crop. + null + } ?: return 0 + return when ( + exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) + ) { + ExifInterface.ORIENTATION_ROTATE_90 -> 90 + ExifInterface.ORIENTATION_ROTATE_180 -> 180 + ExifInterface.ORIENTATION_ROTATE_270 -> 270 + else -> 0 + } + } + private fun openBitmapInputStream(uri: String, headers: HashMap?): InputStream? { return if (uri.startsWith("data:")) { val src = uri.substring(uri.indexOf(",") + 1) @@ -564,25 +590,6 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { return mimeType } - private fun getOrientation(context: Context, uri: Uri): Int { - val file = getFileFromUri(context, uri) - if (file == null) { - return 0 - } - val exif = ExifInterface(file.absolutePath) - return when ( - exif.getAttributeInt( - ExifInterface.TAG_ORIENTATION, - ExifInterface.ORIENTATION_NORMAL - ) - ) { - ExifInterface.ORIENTATION_ROTATE_90 -> 90 - ExifInterface.ORIENTATION_ROTATE_180 -> 180 - ExifInterface.ORIENTATION_ROTATE_270 -> 270 - else -> 0 - } - } - @Throws(IOException::class) private fun copyExif(context: Context, oldImage: Uri, newFile: File) { val oldFile = getFileFromUri(context, oldImage) From 62e03ab0ccf08b0cbb4b1ac98af8b0e1ab209634 Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Thu, 3 Dec 2026 10:34:41 +0100 Subject: [PATCH 2/2] style(android): apply spotless formatting --- .../imageeditor/ImageEditorModuleImpl.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt index 90322f4..04b592e 100644 --- a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt +++ b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt @@ -359,8 +359,9 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { private fun getOrientation(uri: Uri, headers: HashMap?): Int { val exif = try { - openBitmapInputStream(uri.toString(), headers) - ?.let { input -> input.use { ExifInterface(BufferedInputStream(it)) } } + openBitmapInputStream(uri.toString(), headers)?.let { input -> + input.use { ExifInterface(BufferedInputStream(it)) } + } } catch (e: IOException) { // Images without Exif data are not decodable as Exif: treat them as upright // rather than failing the crop.