Skip to content

Commit cb39214

Browse files
authored
Merge pull request #6 from s77rt/support-content-scheme
support content scheme
2 parents 93399c6 + bc564a4 commit cb39214

1 file changed

Lines changed: 36 additions & 14 deletions

File tree

android/src/main/java/com/existfragger/rnimagesize/RNImageSizeModule.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.existfragger.rnimagesize;
22

3+
import android.content.ContentResolver;
34
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
56
import android.media.ExifInterface;
67
import android.net.Uri;
8+
import android.os.Build;
79

810
import com.facebook.react.bridge.Arguments;
911
import com.facebook.react.bridge.Promise;
@@ -18,8 +20,11 @@
1820
public class RNImageSizeModule extends NativeImageSizeModuleSpec {
1921
public static final String NAME = "RNImageSize";
2022

23+
private ReactApplicationContext reactContext;
24+
2125
public RNImageSizeModule(final ReactApplicationContext reactContext) {
2226
super(reactContext);
27+
this.reactContext = reactContext;
2328
}
2429

2530
@Override
@@ -28,30 +33,47 @@ public void getSize(String uri, final Promise promise) {
2833
public void run() {
2934
try {
3035
Uri u = Uri.parse(uri);
36+
String scheme = u.getScheme();
3137
BitmapFactory.Options options = new BitmapFactory.Options();
38+
ExifInterface exifInterface = null;
3239
options.inJustDecodeBounds = true;
3340

3441
int height = 0;
3542
int width = 0;
3643
int rotation = 0;
3744

38-
if (uri.startsWith("file://")) {
39-
BitmapFactory.decodeFile(u.getPath(), options);
40-
ExifInterface exifInterface = new ExifInterface(u.getPath());
41-
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
45+
if (ContentResolver.SCHEME_FILE.equals(scheme) ||
46+
ContentResolver.SCHEME_CONTENT.equals(scheme) ||
47+
ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
48+
ContentResolver contentResolver = reactContext.getContentResolver();
49+
BitmapFactory.decodeStream(contentResolver.openInputStream(u), null,
50+
options);
51+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
52+
exifInterface =
53+
new ExifInterface(contentResolver.openInputStream(u));
54+
} else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
55+
exifInterface = new ExifInterface(u.getPath());
56+
}
57+
} else {
58+
URL url = new URL(uri);
59+
BitmapFactory.decodeStream(url.openStream(), null, options);
60+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
61+
exifInterface = new ExifInterface(url.openStream());
62+
}
63+
}
64+
65+
height = options.outHeight;
66+
width = options.outWidth;
67+
68+
if (exifInterface != null) {
69+
int orientation =
70+
exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
4271
if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
43-
rotation = 90;
72+
rotation = 90;
4473
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
45-
rotation = 180;
74+
rotation = 180;
4675
else if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
47-
rotation = 270;
48-
height = options.outHeight;
49-
width = options.outWidth;
50-
} else {
51-
URL url = new URL(uri);
52-
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) url.getContent());
53-
height = bitmap.getHeight();
54-
width = bitmap.getWidth();
76+
rotation = 270;
5577
}
5678

5779
WritableMap map = Arguments.createMap();

0 commit comments

Comments
 (0)