I need to download image url and store it in cache as bitmap.My qus is is there any config that i have to do inorder to store the image bitmaps in cache..? Is there any need to create any cache file?
You should try this librairy : https://github.com/square/pollexor
Well, you just can put it in the app's cache (context.getCacheDir()). What I'd recommend is to use wasp: a cool Android library to handle Bitmaps safely (safe == avoid OutOfMemory exceptions which are pretty common), based on a LRU cache.
It also allows you to download images in the background and get a callback once your image is downloaded... or automatically set the image to an ImageView once it gets downloaded. It is open-source, and is available for Maven users too.
Disclaimer: I'm one of the developers working on wasp library.
Depending on how many images you want to cache, you can probably get away with an in-memory cache using a LruCache. I'd cache the downloaded byte array, not the Bitmap, because Bitmaps are Big Things and decoding byte array -> Bitmap is pretty fast.
Related
I am loading multiple images locally from devices storage. I am currently using Glide to load them. But Glide is very big library and it offers many features which I don't need at all, for ex. disk cache and loading from internet. Is there any better and more efficient library or another way to load images from devices storage?
The only functionality I need is memory bitmap cache, async loading from Uri and resizing just like in Glide.
You can use Picasso, its method count is ~700 compared to ~3200 of Glide, you can save lots of method count here.
I will recommend you do keep using image library, it does lots of cool stuff in the background, which requires lots of dev efforts.
Resizing of images according to view size
Caching in memory using LRU cache
Recycling of Bitmaps to free up heap once you are done with displaying
Disk cache, so that you can load faster on app restart OR eviction from LRU cache
Proven record of working on fragmented Android ecosystem
No need to take care of threads, thread pools
Also on top of if you are using Proguard (using minifyEnabled true in your build.gradle ) for code obfuscation, it will strip away methods which are not getting used in your apk
I'm using Universal Image Loader for image loading, In the document I can't find a way to set different DiskCache for image display, the DiskCache directory is set by ImageLoader.getInstance().init(), which is global.
But I want to store some images in a separate DiskCache because I don't want it to be removed when LruDiskCache is full or cleared manually, I there anyway to do this?
I am afraid UIL can't store specific image files intently. If I guess right, these images that you want to store in separate directory are used frequently. But, maybe you don't understand the mechanism of LruDiskCache which means "least recently used disk cache". Which means files used frequently would at the header of the queue all the time. So, if you use LruDiskCache, these images used frequently will be always stored in the disk and not be deleted or cleared. So, your requirement has been fulfilled when you use LruDiskCache.
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.
I have an app where people can set pictures as wallpaper but its really slow because pictures are very big. This app load pictures and then when i want to see it again it has to load again.
What to do to make it faster? I use Json service. My pictures are from server. i am using universal image loader.
Theres 2 types of cache in android, the memory cache and the disk cache. You should use picasso its a library that implement both and is very easy to use (1 line of code).
I am using picasso library for loading images .In default picasso, It uses internal cache memory for loading images.But as per my app configuration ,i have to use external cache memory(Cache on Disk).
so i used this code for Cache on Disk
File httpCacheDir = new File(getApplicationContext().getExternalCacheDir(),"http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);}
Picasso is flexible. So now it caches images in external Sd card..
The caches is stored in sdcard/android/data/packagename/cache/http
The caches are stored in ".1" ,".0". formats
so i just opened them and changes into ".1" to ".jpg".it gives exact images what i need.
But how to do in programatically? but picasso itself caches my memory in to my app for loading image into imageview.but i have to save them into sdcard directly as images/set bitmap as wallpaper in offline mode?
You can supply your own Cache implementation when building your Picasso instance. This way you can provide extra methods that you can call to retrieve bitmaps directly from your memory cache. Use Picasso.Builder to provide your own implementation for it. When you use with() you are using a static singleton internal instance thats setup with most of the default values (most apps need the default values anyway.)
Keep a reference of your Cache implementation around and directly interact with it. Picasso is meant to handle the loading/decoding and caching for you but there is no reason you cant build around it.
If you are referring about the disk cache, then no Picasso does not support that at the moment. This is by design because the disk layer cache is done by the HTTP layer and makes no distinction about it.
You could however, change the path of the disk cache. If you are using OkHttpDownloader then supply a different file when you construct your Downloader. Similarly for UrlConnectionDownloader you could extend it and override the load() method.
Picasso does handle the caching in it and downloading also you just need to place it in your target Image view similar to Aquery
According to The Corner Square Engineering blog
picasso handle downloading caching in it self and give its handler to user to use it and place the image in image view