Android Question Compress bitmap (on the fly without saving to file first)

Peter Simpson

Expert
Licensed User
Hello All,
Is there a way to compress an image that is already in a bitmap but WITHOUT first saving it to a compressed image first then loading in again.

Basically I'm looking for a solution that will compress a bitmap without saving it first, the bitmaps are loaded directly from an online MySQL database but needed compressing before putting into a list of ImageViews.

Yes I can use LoadBitmapSample or even LoadBitmapResize but that means first saving the image to a file as a compressed jpg then load it into ImageViews. Doing it that way (saving compressed file) does work but scrolling using xCustomListView with lazy loading causes the list to stagger, especially on slower devices.

Just for TESTING PURPOSED ONLY I tried using LoadBitmap which does load the full sized images directly into the ImageViews lists (not recommended), xCustomListView with lazy loading and it strangely ran smoother but still not perfect, but obviously sooner or later there WILL BE an out of memory issue especially on lesser devices.

So the obvious solution to me is compressing the bitmap on the fly (not saving it first) then placing it directly into the xCustomListView ImageViews.

Saving a compressed jpg and reloading it is not really an option, I'm trying to do it on the fly.

Thank you...

Tested on Galaxy S4, HTC Desire, N5, N6P and Pixel XL, it does the same on all of them. If bitmap.compress existed that might have helped with this issue.

P.S. I also tried implementing Inline Java but I couldn't get it working correctly, so I deleted the code.

Cheers...
 
Last edited:

Peter Simpson

Expert
Licensed User
Hiya @Erel and thanks for the reply.
I'not seen that option before, goodness knows how I missed it. Anyway I've implemented what you mentioned above at it has appears to have improves things somewhat, but I also quickly wrote a vb.net program to load the images out of the clients MySQL database, compress them and save them back but into a new table to make the images smaller but keeping the original Base64 data. Anyway doing that and implementing your suggestion now means that I have an xCustumListView card view with lazy loading and removable cards running as smoothly as the normal xCLV that I now always implement for displaying lists as standard.

And today's lesson is a simple one.
Always make sure that images are the size that your clients say they are. My client said that the images (stored as Base64) were only about 40k in size. A couple of hours ago I finally checked and the images with their sizes, their sizes ranged from between 420k and 1850k. Once my newly created vb.net programmed compressed the images down to size the xCLV with LL populated quickly and scrolled extremely smoothly.

Anyway thank you again...
 
Upvote 0
Top