From bc564a42b99b2d544a783d4146ed456a6e3b9227 Mon Sep 17 00:00:00 2001 From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com> Date: Sun, 1 Sep 2024 18:32:53 +0100 Subject: [PATCH] support content scheme --- .../rnimagesize/RNImageSizeModule.java | 50 +++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/android/src/main/java/com/existfragger/rnimagesize/RNImageSizeModule.java b/android/src/main/java/com/existfragger/rnimagesize/RNImageSizeModule.java index 2adc706..ad003b1 100644 --- a/android/src/main/java/com/existfragger/rnimagesize/RNImageSizeModule.java +++ b/android/src/main/java/com/existfragger/rnimagesize/RNImageSizeModule.java @@ -1,9 +1,11 @@ package com.existfragger.rnimagesize; +import android.content.ContentResolver; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ExifInterface; import android.net.Uri; +import android.os.Build; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Promise; @@ -18,8 +20,11 @@ public class RNImageSizeModule extends NativeImageSizeModuleSpec { public static final String NAME = "RNImageSize"; + private ReactApplicationContext reactContext; + public RNImageSizeModule(final ReactApplicationContext reactContext) { super(reactContext); + this.reactContext = reactContext; } @Override @@ -28,30 +33,47 @@ public void getSize(String uri, final Promise promise) { public void run() { try { Uri u = Uri.parse(uri); + String scheme = u.getScheme(); BitmapFactory.Options options = new BitmapFactory.Options(); + ExifInterface exifInterface = null; options.inJustDecodeBounds = true; int height = 0; int width = 0; int rotation = 0; - if (uri.startsWith("file://")) { - BitmapFactory.decodeFile(u.getPath(), options); - ExifInterface exifInterface = new ExifInterface(u.getPath()); - int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); + if (ContentResolver.SCHEME_FILE.equals(scheme) || + ContentResolver.SCHEME_CONTENT.equals(scheme) || + ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) { + ContentResolver contentResolver = reactContext.getContentResolver(); + BitmapFactory.decodeStream(contentResolver.openInputStream(u), null, + options); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + exifInterface = + new ExifInterface(contentResolver.openInputStream(u)); + } else if (ContentResolver.SCHEME_FILE.equals(scheme)) { + exifInterface = new ExifInterface(u.getPath()); + } + } else { + URL url = new URL(uri); + BitmapFactory.decodeStream(url.openStream(), null, options); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + exifInterface = new ExifInterface(url.openStream()); + } + } + + height = options.outHeight; + width = options.outWidth; + + if (exifInterface != null) { + int orientation = + exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) - rotation = 90; + rotation = 90; else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) - rotation = 180; + rotation = 180; else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) - rotation = 270; - height = options.outHeight; - width = options.outWidth; - } else { - URL url = new URL(uri); - Bitmap bitmap = BitmapFactory.decodeStream((InputStream) url.getContent()); - height = bitmap.getHeight(); - width = bitmap.getWidth(); + rotation = 270; } WritableMap map = Arguments.createMap();