Skip to content

Akash/issues#5

Open
akashrnjn1 wants to merge 5 commits intomainfrom
akash/issues
Open

Akash/issues#5
akashrnjn1 wants to merge 5 commits intomainfrom
akash/issues

Conversation

@akashrnjn1
Copy link
Copy Markdown
Collaborator

No description provided.

This commit introduces optimized bitmap loading in `AddRecordPreviewScreen.kt` to improve performance and prevent potential memory issues when handling large images.

- **Optimized Bitmap Loading:**
    - A new function `loadOptimizedBitmap(filePath: String)` is added.
    - This function first decodes image bounds using `BitmapFactory.Options` with `inJustDecodeBounds = true`.
    - It checks if the estimated byte size of the image exceeds 100MB or if its dimensions are larger than the maximum canvas size (32768 pixels).
    - If the image is too large, `inSampleSize` is calculated using a new `calculateInSampleSize` function, and `inPreferredConfig` is set to `Bitmap.Config.RGB_565` to reduce memory usage.
    - The image is then decoded with the calculated sample size and corrected for orientation using the existing `fixImageOrientation` function.
    - In the `LazyRow` displaying file previews, `BitmapFactory.decodeFile(file.path)?.let { fixImageOrientation(it, file.path) }` is replaced with a call to the new `loadOptimizedBitmap(file.path)`.
- **Sample Size Calculation:**
    - A new private function `calculateInSampleSize(options: BitmapFactory.Options)` is introduced.
    - This function calculates an appropriate `inSampleSize` if the image height is greater than 800 or width is greater than 600, iteratively doubling `inSampleSize` until the scaled dimensions are within reasonable limits.
This commit refactors the `loadOptimizedBitmap` function in `AddRecordPreviewScreen.kt` to improve memory efficiency and prevent crashes due to large images.

- **Bitmap Loading Optimization:**
    - The `loadOptimizedBitmap` function now checks for valid image dimensions (width and height > 0) before proceeding.
    - It uses system display metrics (`Resources.getSystem().displayMetrics`) to determine a requested width and height for downsampling.
    - A `maxBytes` threshold (approximately 80 MB) is introduced as a safe limit for the estimated size of the loaded bitmap.
    - The `calculateInSampleSize` function has been updated to consider the image dimensions, maximum allowed dimensions (constrained by `maxCanvasSize`), and the `maxBytes` limit to determine an appropriate `inSampleSize`. It iteratively doubles `inSampleSize` until the scaled image fits within the defined constraints.
    - The `inPreferredConfig` is set to `Bitmap.Config.RGB_565` to reduce memory usage (2 bytes per pixel).
    - The original bitmap is recycled if a new, rotated bitmap is created in `fixImageOrientation` to free up memory.
- **Error Handling:**
    - The function now returns `null` if the initial image dimensions are invalid or if `BitmapFactory.decodeFile` fails to decode the bitmap.
This commit refactors the `loadOptimizedBitmap` function in `AddRecordPreviewScreen.kt` to improve memory efficiency and prevent crashes due to large images.

- **Bitmap Loading Optimization:**
    - The `loadOptimizedBitmap` function now checks for valid image dimensions (width and height > 0) before proceeding.
    - It uses system display metrics (`Resources.getSystem().displayMetrics`) to determine a requested width and height for downsampling.
    - A `maxBytes` threshold (approximately 80 MB) is introduced as a safe limit for the estimated size of the loaded bitmap.
    - The `calculateInSampleSize` function has been updated to consider the image dimensions, maximum allowed dimensions (constrained by `maxCanvasSize`), and the `maxBytes` limit to determine an appropriate `inSampleSize`. It iteratively doubles `inSampleSize` until the scaled image fits within the defined constraints.
    - The `inPreferredConfig` is set to `Bitmap.Config.RGB_565` to reduce memory usage (2 bytes per pixel).
    - The original bitmap is recycled if a new, rotated bitmap is created in `fixImageOrientation` to free up memory.
- **Error Handling:**
    - The function now returns `null` if the initial image dimensions are invalid or if `BitmapFactory.decodeFile` fails to decode the bitmap.
This commit refactors the `loadOptimizedBitmap` function in `AddRecordPreviewScreen.kt` to improve memory efficiency and prevent crashes due to large images.

- **Bitmap Loading Optimization:**
    - The `loadOptimizedBitmap` function now checks for valid image dimensions (width and height > 0) before proceeding.
    - It uses system display metrics (`Resources.getSystem().displayMetrics`) to determine a requested width and height for downsampling.
    - A `maxBytes` threshold (approximately 80 MB) is introduced as a safe limit for the estimated size of the loaded bitmap.
    - The `calculateInSampleSize` function has been updated to consider the image dimensions, maximum allowed dimensions (constrained by `maxCanvasSize`), and the `maxBytes` limit to determine an appropriate `inSampleSize`. It iteratively doubles `inSampleSize` until the scaled image fits within the defined constraints.
    - The `inPreferredConfig` is set to `Bitmap.Config.RGB_565` to reduce memory usage (2 bytes per pixel).
    - The original bitmap is recycled if a new, rotated bitmap is created in `fixImageOrientation` to free up memory.
- **Error Handling:**
    - The function now returns `null` if the initial image dimensions are invalid or if `BitmapFactory.decodeFile` fails to decode the bitmap.
This commit modifies the `AddRecordPreviewScreen.kt` file to change the maximum canvas size used for image processing.

- The `maxCanvasSize` constant has been reduced from `32768` to `8192`. This change likely aims to optimize memory usage or address potential issues with handling very large images on certain devices.
@skum-14 skum-14 added the wip Work in progress, do not merge this label Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wip Work in progress, do not merge this

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants