How can I get a cached raster file downloaded using Volley ImageLoader?

Problem: I have X ImageViews elements that I add dynamically as follows:

for (int i=2; i < result.size(); i++) { // instantiate image view ImageView mImageView = new ImageView(this); mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER); mImageView.setBackgroundResource(R.drawable.selectable_background_theme); mImageView.setOnClickListener(this); // download image and display it mImageLoader.get(result.get(i), ImageLoader.getImageListener(mImageView, R.drawable.ic_logo, R.drawable.ic_action_refresh)); // add images to container view mLlDescContent.addView(mImageView); } 

What do you want to click on the image and display it in another action in full screen mode. I read about several ways, such as passing a Uri or passing an actual bitmap as a byte array.

Question: How to get the Uri or the actual bitmap that I downloaded using Volley ImageLoader. LruCache I am using BitmapLruCache, which I found here: Android Volley ImageLoader - BitmapLruCache parameter? . Can anyone help me with this or any idea to achieve my goal.

I tried this after the above code and nothing:

 Bitmap mBitmap = VolleyInstance.getBitmapLruCache().getBitmap(result.get(2)); mIvAuthorImg.setImageBitmap(mBitmap); 

Edit: If I re-request the image with

 mImageLoader.get(result.get(i), ImageLoader.getImageListener(mImageView, R.drawable.ic_logo, R.drawable.ic_action_refresh)); 

the image is loaded from the cache, but if I try to access the image directly from the cache with:

 Bitmap mBitmap = VolleyInstance.getBitmapLruCache().getBitmap(result.get(2)); mIvAuthorImg.setImageBitmap(mBitmap); 

image does not load. I want to be able to manipulate an image, such as the size of the material, before passing it on to the next step.

+4
source share
2 answers

Volleyball depends on your cache implementation for successful effective caching. The constructor for ImageLoader is ImageCache , which is Volley’s simple interface for saving and loading bitmap images.

 public ImageLoader(RequestQueue queue, ImageCache imageCache) 

Quote from the Javadoc ImageCache interface:

Simple cache adapter interface. If provided by ImageLoader, it will be used as the L1 cache before being sent to Volley. Implementations should not be blocked. It is recommended to use LruCache.

Darwind is right. If you request an image and it is present in the cache, it will be downloaded from the cache, not from the Internet. This should be for you, since you are uploading and presenting an image that, when clicked, should appear in the cache in your new action.

You say that this does not work, perhaps your implementation is not optimized for your use. What type of cache are you using? Do you have one centralized RequestQueue and ImageLoader , as recommended by the Volley team?

Take a look at this question , which is not exactly the same as yours, but may be useful to you. It has a simple LRU cache implementation.

Hope this helps!

Edit:

The volleyball point is not worried about implementation details. Do you want an image? he will download it for you in the best and fastest way (from memory, and if not online). That is how you should look at it. Fetching the cache and then looking in it for the wrong IMO approach.

Now, if you want to manipulate a bitmap image, you have several options, the best IMO for implementing your own Image Listener pass it to the get() method instead of the standard one.

Something like that:

 public class MyImageListener implements ImageListener() { @Override public void onErrorResponse(VolleyError error) { // handle errors } @Override public void onResponse(ImageContainer response, boolean isImmediate) { Bitmap bitmap = response.getBitmap(); if (bitmap != null) { // // manipulations // // assuming mView is a reference to your ImageView mView.setImageBitmap(bitmap); } else { // display placeholder or whatever you want } } } 

from Javadoc:

Call flow:

  • After joining the request, onResponse (response, true) will be called to reflect any cached data that was already available. If data was available, response.getBitmap () will not be null.

  • After the network response is answered, only one of the following cases will occur:

    • onResponse (response, false) will be called if the image has been uploaded.

      or

    • onErrorResponse will be called if there was an error loading the image.

+5
source

There is an error (I am 70% sure that this is an error) in volley at present, where if the cache expiration is not indicated (say, if you get the image from the S3 bucket, expires), you will always load from the network.

You can get around this by checking out the HttpHeaderParser and changing the corresponding bits (this is not completely crazy since you have to include the source code for the volleyball):

 // Cache-Control takes precedence over an Expires header, even if both exist and Expires // is more restrictive. if (hasCacheControl) { softExpire = now + maxAge * 1000; } else if (serverDate > 0 && serverExpires >= serverDate) { // Default semantic for Expire header in HTTP specification is softExpire. softExpire = now + (serverExpires - serverDate); } else if (serverExpires == 0) { softExpire = Long.MAX_VALUE; } 

Then you can simply pass Uri to the action that opens the item as a parameter. This solution has the enviable property that if something goes wrong and something happens with the image between starting the activity and displaying the bitmap image, you will still restart it, and everything will work properly. This will probably never or rarely happen, but it’s good to know the correctness.

+1
source

Source: https://habr.com/ru/post/1491797/