How to reuse cached image in Glide? - android

Is it possible to keep a reference to the loaded image into cache (on disk) and reuse it later in Glide 4?
Let's say I want to load an image like this:
Future<Bitmap> future = Glide.with(context).asBitmap().load(resourceId).submit(300, 200);
I want to save new processed bitmap to disk. Should I use diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)?
Then in the next activity I want to reuse this new bitmap from disk cache.
How can I do it? Should I use signature as a reference to access this bitmap?

Related

what is the best way to work with Images in SqLite android?

I am working with a customizable database with pictures. Right now I am taking pictures as it is from the sdcard and encoding it in base64 String and then putting it in the database. but whenever I am trying decoding it and showing it in my view, I am getting Out of memory error. Can any one one tell me what is the best procedure to do it? Shall I change the size of the pictures before encoding it?
I want to re-size all of the pictures into 512*512.
Image to Base64 is very heavy operation in android. Consider saving the images on the external/internal memory and save the file path in the sqlite database.
You can convert your image to byte array then store values in sql by using BLOB type and vice versa.
As you mentioned you want to resize the images to 512*512, you can scale the image using below code,
Create bitmap from captured image and then use below line
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 512, 512, false);
It will give you a smaller image, you can also consider compressing the image to reduce in size,
OutputStream imagefile = new FileOutputStream("/your/file/name.jpg");
// Write 'bitmap' to file using JPEG and 50% quality hint for JPEG:
bitmap.compress(CompressFormat.JPEG, 50, imagefile);
Now, you have two options,
Save the scaled and compressed image into a file and save the path of that file in db. (Better way)
Convert the scaled and compressed image to base64 string and save in db.
Althought base64 is , as many answers said, a heavy operation for android, if done properly, it should not be a problem.
There are many reasons a bitmap could be required to be saved to a DB , (photo of a invoice ticket, for example?), and this is the way i do it.
first, create a new , smaller bitmap like #Swapnil commented.
and second, correctly use the bitmap transformation methods, i've been using these (look below) two so far and haven't had any memory issue on many different devices.
link to my BitmapUtils transformation methods

Android Image invalidate in fresco

I am migrating android image caching library from picasso to fresco. I want to know if there is any way to invalidate image already catched as I am adding feature to replace existing image there is way to do so in picasso like
Picasso.with(context).invalidate(URI);
This line remove the cached image and use new one using the url provided which is same like,
http://example.com/image_path
In fresco I have tried using
Fresco.getImagePipeline().evictFromMemoryCache(uri);
This is removing image from view but adding same old cached image again and not getting new one from network as it is working in picasso.
Please refer question Invalidate cache in Picasso The accepted answer doing great in case of picasso.
Fresco.getImagePipeline().evictFromMemoryCache(uri);
Above code line remove the image from the catche but image remains there in the disk and render same if called. We need to remove same image from disk as well. Bellow two lines remove the the image from disc cache also we need to remove the small that is thumbnail image if saved from disk cache.
Fresco.getImagePipelineFactory().getMainDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
Fresco.getImagePipelineFactory().getSmallImageDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
Note: if you are using custom cache key you need to change it that way.
Try this
public static void clearCache(){
//
ImagePipeline imagePipeline = com.facebook.drawee.backends.pipeline.Fresco.getImagePipeline();
imagePipeline.clearMemoryCaches();
imagePipeline.clearDiskCaches();
// combines above two lines
imagePipeline.clearCaches();
}

Image Adapter load method - use Bitmap Array?

For the below - I don not have the URL of the image! I cannot use the URL.
If I am downloading images from the internet into a Bitmap array like this:
Bitmap image = BitmapFactory.decodeStream(file.getReadStream());
//add to the Bitmap Array
images.add(image);
Can this be sent to Picasso (or any other Image loader like UIL?)
I know I can display the images in my Adapter but I am getting out of memory issues. And I thought Picasso could take care of this and the caching etc.
This will not work as it will not accept a Bitmap source. Is there any other library to do this?
Picasso.with(context).load(images.get(position)).resize(60, 60).error(R.drawable.error_icon).into(holder.iconImage);

caching disk image

I am trying to use a disk cache (not a memory cache) so i download my images from an urls and put it in a grid view. I want to download my images only one time.
I found this example (bitmapFun) in google site: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
I found this example a bit complicated.
There are many objects in util package (AsyncTask, DiskLruCache, ImageCache, ImageFetcher, ImageResizer, ImageWorker, Utils)
Is there a way or a tutorial that show how can i use a disk Lru cache without using all those object.
I don't want to resize my image and i was not able to remove ImageResizer class.
Here you have good answer :Android image caching. Quotation :
"Consider using Universal Image Loader library by Sergey Tarasevich. It comes with:
//Multithread image loading. It lets you can define the thread pool size
//Image caching in memory, on device's file sytem and SD card.
//Possibility to listen to loading progress and loading events
Universal Image Loader allows detailed cache management for downloaded images, with the following cache configurations:
UsingFreqLimitedMemoryCache: //The least frequently used bitmap is deleted when the cache size limit is exceeded.
LRULimitedMemoryCache: //The least recently used bitmap is deleted when the cache size limit is exceeded.
FIFOLimitedMemoryCache: //The FIFO rule is used for deletion when the cache size limit is exceeded.
LargestLimitedMemoryCache: //The largest bitmap is deleted when the cache size limit is exceeded.
LimitedAgeMemoryCache: //The Cached object is deleted when its age exceeds defined value.
WeakMemoryCache: //A memory cache with only weak references to bitmaps.
A simple usage example:
ImageView imageView = groupView.findViewById(R.id.imageView);
String imageUrl = "http://domain.com/image.png";
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
imageLoader.displayImage(imageUrl, imageView);
This example uses the default UsingFreqLimitedMemoryCache.

Save image to sdcard from imageview

I have listview having customized some textview and one imageview. When I long click on item I have store that item information to the database but the question is how to store image in sdcard and store the relevant path to the database. The image alread download as cache now I don't want to re-download that image.
Is there way to store that Image to the sdcard.
I am using this example to download the images for listview https://github.com/thest1/LazyList
Edit
I got solution
no need to extra process when using this example. Just store the web path of image into the database and pass that imageview into the ImageLoader object with path it'll use the cache images if the image was exist for same URL
No Need to extra process, Using this example it'll will care for future usage also for same URL of image. This will get from the cache directory if the image found that use that image otherwise download it and then use it.
If you use Prime the caching will be transparent, when you request the image again it will grab it from a memory cache if available or a disk cache automatically. It also is really easy to get images with.
You can save your bitmap which you get via Bitmap bitmap=memoryCache.get(url); using save-file-to-sd-card.
You can also get the bitmap from the ImageView(if you want) like:
//say your ImageView object is i;
i = (ImageView) findViewById(R.id.img);
Drawable d = i.getBackground();
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

Categories

Resources