Get image uri from picasso?
Asked Answered
O

1

16

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://mcmap.net/q/750202/-how-to-retrieve-images-from-cache-memory-in-picasso. 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?

Opsonin answered 11/2, 2014 at 5:13 Comment(3)
Picasso will store the key from the request. The key is built using the URL plus all the unique transformation keys (such as resize, rotate and custom transformations you have).Northeasterly
Hi @Opsonin did you get a solution for this? I am using Picasso with OkHttpDownloader to load the images in sd card. I would like to get the image uri with the given url. Can you please share me your code?Almsgiver
@Noundla Unfortunately, I don't believe I did. I may have used my own LruCache and grabbed it from that. It's been awhile ¯\_(ツ)_/¯Opsonin
O
1

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.

Othilie answered 21/4, 2015 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.