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

correctly handle orientation on android #2058

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
25 changes: 21 additions & 4 deletions android/src/main/java/com/imagepicker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,18 @@ public static void setFrontCamera(Intent intent) {

public static int[] getImageDimensions(Uri uri, Context reactContext) {
try (InputStream inputStream = reactContext.getContentResolver().openInputStream(uri)) {

String orientation = getOrientation(uri,reactContext);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
return new int[]{options.outWidth, options.outHeight};
if (needToSwapDimension(orientation)) {
return new int[]{options.outHeight, options.outWidth};
}else {
return new int[]{options.outWidth, options.outHeight};
}

} catch (IOException e) {
e.printStackTrace();
return new int[]{0, 0};
Expand All @@ -168,7 +176,7 @@ public static int[] getImageDimensions(Uri uri, Context reactContext) {

static boolean hasPermission(final Activity activity) {
final int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
return writePermission == PackageManager.PERMISSION_GRANTED ? true : false;
return writePermission == PackageManager.PERMISSION_GRANTED;
}

static String getBase64String(Uri uri, Context reactContext) {
Expand All @@ -189,6 +197,11 @@ static String getBase64String(Uri uri, Context reactContext) {
}
}

private static boolean needToSwapDimension(String orientation){
return orientation.equals(String.valueOf(ExifInterface.ORIENTATION_ROTATE_90))
|| orientation.equals(String.valueOf(ExifInterface.ORIENTATION_ROTATE_270));
}

// Resize image
// When decoding a jpg to bitmap all exif meta data will be lost, so make sure to copy orientation exif to new file else image might have wrong orientations
public static Uri resizeImage(Uri uri, Context context, Options options) {
Expand All @@ -204,10 +217,14 @@ public static Uri resizeImage(Uri uri, Context context, Options options) {
try (InputStream imageStream = context.getContentResolver().openInputStream(uri)) {
String mimeType = getMimeType(uri, context);
Bitmap b = BitmapFactory.decodeStream(imageStream);

b = Bitmap.createScaledBitmap(b, newDimens[0], newDimens[1], true);
String originalOrientation = getOrientation(uri, context);

if (needToSwapDimension(originalOrientation)) {
b = Bitmap.createScaledBitmap(b, newDimens[1], newDimens[0], true);
}else {
b = Bitmap.createScaledBitmap(b, newDimens[0], newDimens[1], true);
}

File file = createFile(context, getFileTypeFromMime(mimeType));

try (OutputStream os = context.getContentResolver().openOutputStream(Uri.fromFile(file))) {
Expand Down