Skip to content

Commit 2d9b4d2

Browse files
authored
Android: get EXIF orientation from content:// URI on r24+ (#274)
Android has restricted access to the file:// URIs underlying a shared content URI a while ago. I'm not sure when exactly, and my google-fu is weak tonight. On the other hand, they introduced ExifInterface(InputStream) in r24. This can be used even when we do not have permissions / access to the original file:// URI that the content:// URI resolves to. So if we are on Android r24+, we use this new API to obtain the EXIF image orientation and to fix portraits being sideways.
1 parent 509a08d commit 2d9b4d2

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

android/src/main/java/com/reactnativeimageresizer/ImageResizer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.graphics.Matrix;
99
import androidx.exifinterface.media.ExifInterface;
1010
import android.net.Uri;
11+
import android.os.Build;
1112
import android.provider.MediaStore;
1213
import android.util.Base64;
1314
import android.util.Log;
@@ -317,6 +318,12 @@ public static boolean copyExif(Context context, Uri imageUri, String dstPath){
317318
*/
318319
public static int getOrientation(Context context, Uri uri) {
319320
try {
321+
// ExifInterface(InputStream) only exists since Android N (r24)
322+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
323+
InputStream input = context.getContentResolver().openInputStream(uri);
324+
ExifInterface ei = new ExifInterface(input);
325+
return getOrientation(ei);
326+
}
320327
File file = getFileFromUri(context, uri);
321328
if (file.exists()) {
322329
ExifInterface ei = new ExifInterface(file.getAbsolutePath());

0 commit comments

Comments
 (0)