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

Fix: Bitmap transformation with size limit #1017

Conversation

chrispader
Copy link

@chrispader chrispader commented Nov 21, 2023

Original PR by @ArekChr: Expensify#6


This pull request introduces a new transformation method for handling Bitmap objects with a size limit. It provides an implementation that scales down the input Bitmap if its byte count exceeds the specified threshold.

This pull request addresses an issue encountered when dealing with large images on Android devices with hardware acceleration enabled. The issue manifests as a java.lang.RuntimeException: Canvas: trying to draw too large(xxx,xxx,xxx bytes) bitmap error. By introducing a new transformation method, this pull request offers a solution to display large images while still accommodating the specified size limit.

Changes Made:

Added a new class ResizeTransformation, transform, to perform the Bitmap transformation.
The transform method uses a BitmapPool, the transform Bitmap, and output width and height as parameters.
If the transform Bitmap's byte count exceeds the predefined MAX_BYTES threshold:
A scaling factor is calculated based on the square root of the ratio between MAX_BYTES and the transform Bitmap's byte count.
The new width and height are computed by multiplying the output width and height by the scaling factor.
The Bitmap.createScaledBitmap() method creates a scaled Bitmap with new dimensions.
The scaled Bitmap is returned as the result of the transformation.
If the transform Bitmap's byte count is within the MAX_BYTES threshold:
The original toTransform Bitmap is returned as it is.

Overall, this pull request aims to provide a reusable and efficient Bitmap transformation implementation that handles size limitations, allowing for optimized memory usage when working with large images.

Related issues:
Expensify/App#17427
Expensify/App#18963

@cbjs
Copy link

cbjs commented Nov 27, 2023

nice!

@Julesssss
Copy link

Julesssss commented Jan 11, 2024

Hi @chrispader. I'm interesting in trying to solve the Android large image crash for Expensify/App and it looks like this was a good candidate until recently. Is this still a possible fix, or has it become deprecated with the recent merging of Expo-Image?

@chrispader
Copy link
Author

Hi @chrispader. I'm interesting in trying to solve the Android large image crash for Expensify/App and it looks like this was a good candidate until recently. Is this still a possible fix, or has it become deprecated with the recent merging of Expo-Image?

Yea, with react-native-fast-image this fixed the problem. I'd have to look into the expo-image code and see how it's possible there. But i think since they also use Glide, it shouldn't be a problem.

@Julesssss
Copy link

Hey agian @chrispader. I agree that this isn't necessary for us anymore. But we do have the same native Canvas crash with Expo-Image which I'm working on here.

@chrispader
Copy link
Author

Hey agian @chrispader. I agree that this isn't necessary for us anymore. But we do have the same native Canvas crash with Expo-Image which I'm working on here.

i'm adding a comment in the issue there, because i'm not quite sure we mean the same thing.

@numandev1
Copy link
Contributor

@chrispader thanks

@chrispader
Copy link
Author

Closing this PR since i don't think there's much traffic going on in this repo and this will probably not be merged anytime soon.

@Julesssss since we migrated to Expo-Image in Expensify, i don't think we're gonna need to keep this open either...

@chrispader chrispader closed this Mar 27, 2024
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.

None yet

6 participants