Skip to content

Commit

Permalink
Minor Bug fix rotating images
Browse files Browse the repository at this point in the history
  • Loading branch information
v4lpt committed Sep 23, 2024
1 parent 84af92b commit 9265db4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
52 changes: 49 additions & 3 deletions app/src/main/java/v4lpt/vpt/f036/esw/ImageStorageHelper.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,22 +20,65 @@ 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());
return null;
}
}

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);
}
Expand Down
2 changes: 2 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/111.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1.1.1 - minor bug fix
- portrait images now don't get rotated if you save them

0 comments on commit 9265db4

Please sign in to comment.