Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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();
Expand Down