Skip to content

Resize large images on import#199

Merged
Ethran merged 2 commits intomainfrom
dev
Feb 5, 2026
Merged

Resize large images on import#199
Ethran merged 2 commits intomainfrom
dev

Conversation

@Ethran
Copy link
Copy Markdown
Owner

@Ethran Ethran commented Feb 5, 2026

When importing images, decode them to a capped resolution before saving to reduce file size and memory usage.

Should fix issue #190

When importing images, decode them to a capped resolution before saving to reduce file size and memory usage.

Should fix issue #190
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses issue #190 which caused crashes when attempting to draw excessively large bitmaps on the Android canvas. The solution introduces automatic image resizing during import to prevent memory exhaustion and canvas size limit violations. Images are now decoded with a maximum resolution based on screen dimensions (minimum 2048 pixels, or 2x the screen dimensions, whichever is larger) while preserving aspect ratios.

Changes:

  • Added image resizing logic using ImageDecoder API to cap resolution at import time
  • Refactored file handling to separate image-specific processing from generic file copying
  • Added helper functions for MIME type detection and filename extraction

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 9 comments.

File Description
app/src/main/java/com/ethran/notable/io/FileUtils.kt Implements new saveImageFromContentUri function with ImageDecoder-based resizing, extracts getFileNameFromUri helper, and updates createFileFromContentUri to use the extracted helper
app/src/main/java/com/ethran/notable/data/dbUtils.kt Updates copyImageToDatabase to always use the new resizing function, and copyBackgroundToDatabase to conditionally resize images while copying other files unchanged

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +89 to +90
val quality = if (outputFormat == Bitmap.CompressFormat.PNG) 100 else 90
resizedBitmap.compress(outputFormat, quality, out)
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WEBP format uses the quality parameter (90) but this only applies to lossy compression. If the deprecated WEBP constant defaults to lossless compression, the quality parameter will be ignored. This could result in larger file sizes than expected. Consider explicitly using WEBP_LOSSY to ensure the quality setting is respected.

Copilot uses AI. Check for mistakes.

mime.equals("image/webp", ignoreCase = true) || destFile.extension.equals(
"webp", true
) -> Bitmap.CompressFormat.WEBP
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Bitmap.CompressFormat.WEBP constant is deprecated in API 30 and should be replaced with WEBP_LOSSY or WEBP_LOSSLESS for better control over compression. Since the app's minSdk is 29, consider using WEBP_LOSSY with appropriate quality setting or WEBP_LOSSLESS for transparency preservation.

Suggested change
) -> Bitmap.CompressFormat.WEBP
) -> Bitmap.CompressFormat.WEBP_LOSSY

Copilot uses AI. Check for mistakes.
destFile.outputStream().use { out ->
val quality = if (outputFormat == Bitmap.CompressFormat.PNG) 100 else 90
resizedBitmap.compress(outputFormat, quality, out)
out.flush()
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling flush() before the output stream is closed is redundant when using the use() block, as close() automatically flushes the stream. The explicit flush() call can be removed for cleaner code.

Suggested change
out.flush()

Copilot uses AI. Check for mistakes.
@Ethran Ethran merged commit d53f4db into main Feb 5, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants