diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 86466ff..fc0ea4a 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -10,8 +10,8 @@ android { applicationId = "v4lpt.vpt.f036.esw" minSdk = 26 targetSdk = 34 - versionCode = 110 - versionName = "1.1.0" + versionCode = 111 + versionName = "1.1.1" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/v4lpt/vpt/f036/esw/ImageStorageHelper.java b/app/src/main/java/v4lpt/vpt/f036/esw/ImageStorageHelper.java index 17937fa..b5a8b0f 100644 --- a/app/src/main/java/v4lpt/vpt/f036/esw/ImageStorageHelper.java +++ b/app/src/main/java/v4lpt/vpt/f036/esw/ImageStorageHelper.java @@ -1,11 +1,14 @@ package v4lpt.vpt.f036.esw; - import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.media.ExifInterface; import android.net.Uri; import android.util.Log; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; @@ -17,15 +20,40 @@ public class ImageStorageHelper { public static String saveImageToInternalStorage(Context context, Uri imageUri) { try { InputStream inputStream = context.getContentResolver().openInputStream(imageUri); - Bitmap bitmap = BitmapFactory.decodeStream(inputStream); + + // Read the entire input stream into a byte array + ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); + int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + int len; + while ((len = inputStream.read(buffer)) != -1) { + byteBuffer.write(buffer, 0, len); + } + byte[] imageData = byteBuffer.toByteArray(); + + // Read the EXIF data + ExifInterface exif = new ExifInterface(new ByteArrayInputStream(imageData)); + int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); + + // Decode the image + Bitmap originalBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); + + // Rotate the bitmap if needed + Bitmap rotatedBitmap = rotateBitmapIfNeeded(originalBitmap, orientation); String fileName = "event_image_" + UUID.randomUUID().toString() + ".jpg"; File file = new File(context.getFilesDir(), fileName); FileOutputStream fos = new FileOutputStream(file); - bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos); + rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos); fos.close(); + // Recycle bitmaps to free up memory + if (rotatedBitmap != originalBitmap) { + originalBitmap.recycle(); + } + rotatedBitmap.recycle(); + return file.getAbsolutePath(); } catch (Exception e) { Log.e(TAG, "Error saving image: " + e.getMessage()); @@ -33,6 +61,24 @@ public static String saveImageToInternalStorage(Context context, Uri imageUri) { } } + private static Bitmap rotateBitmapIfNeeded(Bitmap bitmap, int orientation) { + Matrix matrix = new Matrix(); + switch (orientation) { + case ExifInterface.ORIENTATION_ROTATE_90: + matrix.postRotate(90); + break; + case ExifInterface.ORIENTATION_ROTATE_180: + matrix.postRotate(180); + break; + case ExifInterface.ORIENTATION_ROTATE_270: + matrix.postRotate(270); + break; + default: + return bitmap; + } + return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); + } + public static Bitmap loadImageFromInternalStorage(String path) { return BitmapFactory.decodeFile(path); } diff --git a/fastlane/metadata/android/en-US/changelogs/111.txt b/fastlane/metadata/android/en-US/changelogs/111.txt new file mode 100644 index 0000000..4604e0e --- /dev/null +++ b/fastlane/metadata/android/en-US/changelogs/111.txt @@ -0,0 +1,2 @@ +1.1.1 - minor bug fix +- portrait images now don't get rotated if you save them