I'm using NOSTRA's Universal Image Loader library and I have caching in memory enabled. It seems that the bitmaps are cached using the URI set in the displayImage() method as the key. As a workaround I created another HashMap that contains the URIs for the images, with a unique id associated with it as the key. The reason for that is the id value is also used to load data in other places, so that value is unique for each image.
So is there a way to change the key for the cached bitmap to something other than the URI?
Bitmap are cached in memory cache by key pattern [imageUrl]_[width]x[height]. There is no way to change this pattern in current lib version (1.7.0).
# NOSTRA :
Can we do like this.
instead of passing [imageUrl]_[width]x[height] while generating cachekey we can just pass our unique ID for cache key.
Related
Some information about the app:
The app contains a lots of images in drawable folder.
The size of every image is between 1kb and 3kb.
And I need to draw them into a ListView.
What I need to get:
I need store the identifier of the image (R.drawable.my_image) into database with some aditional user information.
The problem:
When I update the app (adding new image resources), the identifier may change. So, in database I will have an outdated identifier, which is linking to a non-existing resource.
I've thought to store the resource name in database instead of the resource identifier and them I will retrieve it by getResources().getIdentifier(String, String, String) method. But I don't know if this method is efficient enough to use it in a ListView.
The documentation says Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Do you know any other solution?
Finally Im using getResources().getIdentifier() method to load images into list (with recyclerview). My list has about 20 images. The average size is also less than 3kb. I've tried the app in older devices (wich behavior can be affected by his CPU) and it worked fine. I know this method is not recomended, but can be a solution.
Not efficient to use it in a list view.
Official documentation of Resources.getIdentifier method:
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
NOTE in documentation
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
I am using an ArrayAdapter along with ListView to display some images (I use Picasso to help with image handling). The images are initially loaded from the state on the local device. The adapter has ids for the images, which the getView() method of the adapter uses to get the path. If the image is not available locally, the local state returns an URL for the image. If the URL has expired, I need to fetch this from the backend.
If the URL is not expired, I have no issues, as I can either directly provide the path to Picasso or the URL and Picasso will handle everything for me. However, when I need to fetch the URL from the backend, I need to do this asynchronously. After fetching the URL, I tried doing the following but it doesn't seem to work (my getView() will in this case use Picasso with the URL):
adapter.remove(id);
adapter.add(id);
notifyDataSetChanged() doesn't seem to do much as well.
What is the best way to handle this situation? Is there any other way than what I am trying to do with the adapters? I want to delay the URL fetch from the backend as much as possible, basically until the user needs to see the image.
UPDATE: for now, I am replacing the id with a temp_id when the URL is expired and then add the correct id again, when I have the valid URL so that the view gets refreshed.
Have you tried clearing your ArrayList of image urls with myArray.clear()? Not so sure if I get your question or how your code looks like.
But I have issues like this before. What worked for me was to repopulating the adapter completely.
This link should help.
I am very new to Android development.
I am running into issues getting the correct bitmap from my LruBitmapCache. I use the UUID to generate a unique Id, I don't use a URL because I am generating the bitmap on a canvas. (I'm copying getDrawingCache() from my view to a new bitmap (using copy) then storing the copy into the cache).
For some reason after I store a bitmap, when I access it, it ends up being a bitmap for another id ... this happens sometimes. Any ideas?
I use a currentIndex to track which position I am at in the cachedKeys Array (I want to sequentially save and navigate through the bitamp on a canvas). I think I'm updating the entry in the cache wrong...
A fellow developer assisted. I was doing mDrawingView.restartDrawingCache(); incorrectly and in the wrong order.
It should have been:
<drawing view>.setDrawingCacheEnabled(true);
Bitmap mmap= <drawing view>.getDrawingCache();
Bitmap copy = mmap.copy(mmap.getConfig(), false);
<drawing view>.setDrawingCacheEnabled(false);
Cheers, maybe this will help someone out :)
I have a fairly large list of image URL's that I'm using to load up a ViewPager using Picasso. I need to be able to provide sharing capabilities for these images via an intent (ultimately sharing via ShareActionProvider). From what I've read, Picasso isn't really built to handle this sort of thing out of the box, though it provides all the necessary tools to do so.
My plan before research was to create a simple LruCache which uses the url as the key and bitmap value. This caching would occur in onBitmapLoaded via Picasso's Target interface. Whenever I want to share an image, I'll check the cache for the bitmap. If it's not there, I'll fetch with Picasso. Now that I have a cached bitmap regardless, I'll write to a file (...this part doesn't seem right, though I have to write to a file to get a uri, right?) and add the file uri to the intent.
However I see that with the Picasso.Builder I can set (and retain a reference to) my own cache - https://stackoverflow.com/a/18552559/413254. This means I could do away with the custom Target and confusion with properly implementing hashCode and equals methods to ensure accurate recycling, retrieval, etc.
My question is, how does Picasso use this cache? What are the keys? Is there a way to get a bitmap Uri without writing it to disk?
If you want to use ShareActionProvider to share the image on the current page you don't have to keep your own cache of images. But to be able to share it to others, the image should be in the shared file system on the device.
It would be better if you use image loading libraries with custom disk cache support like Universal Image Loader
If you want to use Picasso (which is a good decision).
You either have to save a copy of the image on every page change which is nor a good option.
Or you can give a custom network handler to Picasso and set a custom cache implementation to it. I would suggest using OkHttp with custom caching which stores files in the format you desire. When you do that, you have to have a function that converts image URLs to file path on a device.
In every page change, if you have a Fragment inside your ViewPager, put the ShareActionProvider into your Fragments.
Get the reference of the ShareActionProvider inside onCreateOptionsMenu. And then set the Intent with the file path you get.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(Utils.getFilePath(imageUrl));
shareIntent.setData(phototUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
mShareActionProvider.setShareIntent(intent);
Edit:
The other option I would prefer is to ditch ShareActionProvider and use a normal menu item for this. The problem with ShareActionProvideris that you have to make the share Intent ready for user to share before hand. You have to make it ready even the user won't share it.
But when you have a normal button, it is much easier because you only make the operation when the user clicks the share button. In that case you can simply request the image one more time from Picasso with a Target object and write the Bitmap you got to a file in the shared external file system and share it.
When I pass in my Image URI to imageLoader.displayImage, it is automatically getting appended with _[W]x[H] at the end of each url. Is there a way to override that string, or prevent it from getting appended at all?
The API i am calling already has querystring parameters setup for height and width. It would be great if I could override this value.
Thank you.
This appendix is used only for memory cache. So different sizes of the same image can be cached in memory. This appendix isn't used for HTTP requests.