Using Bitmap for android consuming lot of memory - android

In my android app,I am displaying various images from gallery whose paths are stored in a database and on runtime it uses bitmap to display image from these paths and due to this it is consuming lot of memory.It is crashing in many mobiles although I am reducing the quality of image. Is there any other way to do this?

I'd strongly suggest looking into image libraries such as Picasso and Glide. Displaying bitmaps natively in Android requires a fair amount of coding to ensure issues like Outofmemory error do not occur.

Related

Android Picasso, using cache

I have a photo gallery(list) in my app, and I use the Picasso library to display images.
But while using the app, the app crashed sometimes because of the Out of memory error. And after searching a web, I found the solution which is to disable using cache for Picasso library.
.memoryPolicy(MemoryPolicy.NO_STORE, MemoryPolicy.NO_CACHE)
After disabled using cache, the error is gone, but I think there is some smart way to using the cache.
Is there any guild line for using Picasso cache? like if the memory usage is closing to limit then, disable the using cache?
As I know, the Out of memory means that the application's memory limit is reached, maybe you try to load a really big image, or the memory for your app is not enough.
Maybe the top answer in this question can help you Simple Android App - Out of Memory error when running on smartphone

Sending byte[] or URL for Image

I'm creating an Android app. For this I created a recyclerview, which gets filled by images and texts. Until now I've got the images from my server with sockets and byte[]. In my app I converted this to bitmaps. But that has not the best performance. Should I get the images with Picasso and URL? Has this a better performance?
Generally in the android or iOS app from the API's we have to get the image url. By using image loader library's we will load that data.
For the android most of the developers prefer universal image loader or picasso for loading their images.
It is best to rely on image loader library like Picasso or UniversalIL as these libraries will manage caching, downsampling and even error handling. Android is very sensitive about bitmap. Without proper management, there will be a lot of exception including the infamous one: BitmapOutOfMemory.
There are 3 locations where images are stored. Their comparison on Memory space availability and Accessibility speed is given below:
Server (Memory Space is HIGH but Accessibility is SLOW)
Hard Disk on Mobile (Memory Space is MEDIUM and Accessibility is MEDIUM)
Heap Memory (Memory Space is LOW and Accessibility is FAST)
We need to create a balance among above 3 locations for optimum utilization of accessibility and memory space.
Use any lazy loading library like picasso or UIL and you don't have to worry about any of these points.It will handle everything itself :)

how to cache images?

I am writing an android app which will have an image feed, something like in for example the instagram app. My question is how can I cache these images so i dont get an out of memory exception?
I have read some tutorials, but all of them are caching Bitmaps in LruCache. I might be wrong but as I think the bitmap in the ImageView and a cached one use the same ammount of memory.
I'm thinking about storing the compressed images (for example JPEG) in an in-memory cache (and of course on the disk) and showing it only when it is visible on the screen, but then the CPU will eat up the battery as it will constantly clearing the ImageView when it's not visible and decompressing the image and showing it when it is in the viewport. And I'm not really sure that the scrolling will be lagless, even if I do it on a new thread.
An alternative is to do the same as I described above, but I wont remove the bitmap from the imageview immediately, only when there are a lot of images and i will run out of memory.
What do you think?
Here is an example step-by-step on how to cache images, in memory and disk:
http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134
But you can also use libs that already work pretty well like :
http://square.github.io/picasso/
The first link also contains explanation on how you should treat bitmaps to avoid outOfMemory.

Using SVG files in android and memory usage

My application has an activity with metro schema, so for good quality I need to put image with resolution more then 2000x2000, but such images use much memory and application crashes with Out of memory error.
Does this problem will solve when I will change the image source to SVG (by svg-android for example)?
It depends on the complexity of image simple vector re presentable image will defiantly improve the performance otherwise complex images will use more memory.
more Details here.

Android application OutOfMemory. Bitmap

I have an application for android 3.0 and higher where i have to show some large bitmaps.The bitmaps are already scaled to the size in which they have to be displayed. I have already used the largeheap=true in manifest file but still i am getting OutOfMemory error. I am not using LRU cache right now as mention in displaying bitmaps efficiently on developer.android. I have the following doubts.
Should i just try-catch the error and do the rest of the work?
Do i have to use bitmap.recycle() function. After android 3.0 it is not necessary to use bitmap.recycle() as the memory is not allocated in native and as mentioned this function frees the native object.
Should i use the LRU cache mechanism as mention in caching bitmaps ? Wont it increase the memory consumption of my application as i would be storing bitmaps in cache ?
Is there any proper example or architecture to display bitmaps that explain the LRUcache mechanism. Reading all the theories on the net is alot confusing.
Does the inBitmap field of BitmapFactory.Options also help in reducing memory ?
I have memory constraints on my application and i want it to use as less memory as possible. Please advise.
The best way to manage the memory and keep the quality is explained in this article on developer android Displaying Bitmaps Efficiently . Hope this answers your query.

Categories

Resources