Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image dimensions info when the file has some EXIF orientation (Android) #343

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
Expand All @@ -25,7 +26,6 @@

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.GuardedAsyncTask;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
Expand Down Expand Up @@ -637,9 +637,7 @@ private static boolean putPlayableDuration(
// Do nothing. We can't handle this, and this is usually a system problem
}
try {
int timeInMillisec =
Integer.parseInt(
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
int timeInMillisec = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
playableDuration = timeInMillisec / 1000;
} catch (NumberFormatException e) {
success = false;
Expand Down Expand Up @@ -684,19 +682,16 @@ private static boolean putImageSize(
}

boolean success = true;
@Nullable AssetFileDescriptor photoDescriptor = null;

/* Read height and width data from the gallery cursor columns */
int width = media.getInt(widthIndex);
int height = media.getInt(heightIndex);

/* If the columns don't contain the size information, read the media file */
if (width <= 0 || height <= 0) {
@Nullable AssetFileDescriptor photoDescriptor = null;
try {
photoDescriptor = resolver.openAssetFileDescriptor(photoUri, "r");
} catch (FileNotFoundException e) {
success = false;
FLog.e(ReactConstants.TAG, "Could not open asset file " + photoUri.toString(), e);
}

if (photoDescriptor != null) {
if (isVideo) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
Expand All @@ -705,12 +700,8 @@ private static boolean putImageSize(
// Do nothing. We can't handle this, and this is usually a system problem
}
try {
width =
Integer.parseInt(
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
height =
Integer.parseInt(
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
} catch (NumberFormatException e) {
success = false;
FLog.e(
Expand All @@ -722,21 +713,45 @@ private static boolean putImageSize(
retriever.release();
} else {
BitmapFactory.Options options = new BitmapFactory.Options();
// Set inJustDecodeBounds to true so we don't actually load the Bitmap, but only get its
// dimensions instead.
// Set inJustDecodeBounds to true so we don't actually load the Bitmap in memory,
// but only get its dimensions
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(photoDescriptor.getFileDescriptor(), null, options);
width = options.outWidth;
height = options.outHeight;
}
} catch (FileNotFoundException e) {
success = false;
FLog.e(ReactConstants.TAG, "Could not open asset file " + photoUri.toString(), e);
}
}

try {
photoDescriptor.close();
} catch (IOException e) {
// Do nothing. We can't handle this, and this is usually a system problem
/* Read the EXIF photo data to update height and width in case a rotation is encoded */
if (success && !isVideo && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
if (photoDescriptor == null) photoDescriptor = resolver.openAssetFileDescriptor(photoUri, "r");
ExifInterface exif = new ExifInterface(photoDescriptor.getFileDescriptor());
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
if (rotation == ExifInterface.ORIENTATION_ROTATE_90 || rotation == ExifInterface.ORIENTATION_ROTATE_270) {
// swap values
int temp = width;
width = height;
height = temp;
}
} catch (FileNotFoundException e) {
success = false;
FLog.e(ReactConstants.TAG, "Could not open asset file " + photoUri.toString(), e);
} catch (IOException e) {
FLog.e(ReactConstants.TAG, "Could not get exif data for file " + photoUri.toString(), e);
}
}

if (photoDescriptor != null) {
try {
photoDescriptor.close();
} catch (IOException e) {
// Do nothing. We can't handle this, and this is usually a system problem
}
}

image.putInt("width", width);
Expand Down