Sunday, March 29, 2020

Common Mistake with Images in Android


content taken from https://www.toptal.com/android/top-10-most-common-android-development-mistakes

Not understanding Bitmaps

Users love content! Especially when the content is well formatted and looks nice. Images, for instance, are extremely nice content, mainly due to their property of conveying a thousand words per image. They also consume a lot of memory. A lot of memory!
Before an image is displayed on the screen, it has to be loaded into the memory. Since bitmaps are the most common way to do this, we’re going to provide an Android programming guide for the whole process:
Let’s say you want to display an image on your screen that you just took with your camera. The total memory needed for this is calculated with the following formula: memory_needed_in_bytes = 4 * image_width * image_height;
Why 4? Well, the most common / recommended bitmap configuration is ARGB_8888. That means that for each pixel we draw, we need to keep 8 bits (1 byte) for the alpha, the red, the greed and the blue channel in memory, in order to properly display it. There are alternatives, like the RGB_565 configuration that requires half the memory than ARGB_8888, but loses the transparency and the color precision (while maybe adding a green tint).
Let’s assume you have a brand new device with full HD screen and 12 MP camera. The picture you just took is 4000x3000 pixels large and the total memory needed to display it is: 4 bytes * 4000 * 3000 = 48 MB
48 megabytes of your RAM just for a single image!? That’s a lot!
Now let’s take the screen resolution into consideration. You are trying to show a 4000x3000 image on a screen that has 1920x1080 pixels, in worst case scenario (displaying the image full screen) you shouldn’t allocate more than 4 * 1920 * 1080 = 8.3 MB of memory.
Always follow the Android programming tips for displaying bitmaps efficiently:
  1. Scale / crop the large image accordingly : - This action resolve many issues when are you dealing with images in android. 
  2. Issue No 1 - Imageview is not displaying images due to high resolution or larger dimensions images. Like Image of Resolution 4608*2592 is not displaying in my imageview app.
  3. Issue No 2 - Gridview with images taking too much space in memory therefor App crashes due to out of memory.
  4. Issue No 3 - Taking reasonable time during saving  high resolution or larger dimensions images.
  5. Issue No 4 - Taking reasonable time during displaying images.

Solution:
compress the image like whatsapp https://stackoverflow.com/a/34367979
https://gist.github.com/vipulasri/0cd97d012934531f1266#file-imagecompression
https://abdelhady.net/2015/03/28/android-loading-images-super-fast-like-whatsapp-part-2/

Note:- bitmapImage.compress(Bitmap.CompressFormat.JPEG, 85, output)
dont use PNG in compressformat. PNG which is lossless, will ignore the quality setting. PNG have 0*0 resolution. You cannot compress if your image source is png.

If you are using PNG format then it will not compress your image because PNG is a lossless format. use JPEG for compressing your image and use 0 instead of 100 in quality.
Quality Accepts 0 - 100
0 = MAX Compression (Least Quality which is suitable for Small images)
100 = Least Compression (MAX Quality which is suitable for Big images)


create thumbnail of image via inbuilt class ThumbnailUtils like
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(myBitmap,300,300);

1 comment:

  1. Java is the first choice of many software developers for writing applications for the enterprise. The application of java is essential for enterprise application development. Java Enterprise Edition (Java EE) is a very popular platform that provides API and runtime environment for scripting. It also includes network applications and web services. JavaEE is also considered the backbone for various banking applications that have Java running on the UI to back server end

    ReplyDelete