Picasso - fetch image with additional information - android

I need pre-load images and save then to cache with additional information. Is there some easy solution?
My code for saving picture looks:
Picasso.with(mC.get()).load(image.getSrc()).fetch();
And I am searching for something like
Picasso.with(mC.get()).load(image.getSrc()).fetch().withAddInfo(image.getInfo());
With later use:
String info = Picasso.with(mC.get()).load(image.getSrc()).getAddInfo();
Is there a way?

Related

Which is the better way to pass Image between 2 activities? Saving to file or Caching?

I know this is a very frequently asked questions and there are multiple answers. My question is "which is better?"
I've an activity where I capture an image using camera. I need to pass this image to another activity.
One way is to create Bitmap and pass it in putExtra since Bitmap is a Parcelable. This fails when the image size is too big.
I found 2 solutions. This answer uses MemoryCache to save and retrieve the image. Many answers (this, this and this) recommend to save the image to storage and then pass the path to new activity and read the image there.
Which is a better method in this case? (In terms of speed and memory)
It is safer in any case work with path or link than pass it as it is. It works not only with images but with most types of data. At the same time while working with path you can easy make if/else checks and handling some unexpected scenarios. Also pass the path is much faster.

get bitmap after picasso sets image to an ImageView

I'm trying to save downloaded images in shared preferences in base64 format so that the app can work offline. I've used picasso
Picasso.with(mContext).load(urls.get(position)).into(imageView);
I request the server with a specific category of images and the server responds with a json containing the urls of those images. I found picasso to do this job for me as i need to write my async task for this.The images are loaded into ImageViews of a gridview image adapter.
What I want to do is that as soon as the image is loaded through the url using this function I want that bitmap image so that I can save it in shared preferences or serialize it something so that it can be used offline.
or suggest me a better solution.
Solved it!
Picasso.with(mContext)
.load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView);
Thankyou so much guys <3

Is there anyway to synchronously retrieve bitmap from Glide on main thread?

I would like to use the awesome Glide cache but not finding a way to synchronously check if an image is available and pull the bitmap if it is. I only see async methods. Am I missing something? I want the bitmap then a byte array, and have no plans to put it in an imageview. If its in the cache I want to sync obtains its byte array. Can this be done? I saw this: https://github.com/bumptech/glide/issues/495
But that just deals with setup issue for an imageview not really what I need.

How get all image in folder in my site to listview?

Hello everyone i using this Lazylist , i need this lazy read all image in one path.
example :
i have this path http://www.example.com/image
in this path There are many image
http://www.example.com/image/image1.jpg
http://www.example.com/image/sacimge.jpg
http://www.example.com/image/xxx.jpg
and so on ...
need Lazy read all image in this path and insert in My Listview why How do I do that ???
Unfortunately I don't think this is possible. There is no way for lazyList to know how many images you have and what are their url. I suggest you create webservice
like
http://www.example.com/image/cat
Returning a json or xml array containing all the urls of you images.
On server side you can access your image directory and build this list dynnamicaly.
Then in your app you can call this webservice using (retrofit lib to make it easy for you). Then you can feed this url list into your LazyList.

Get image uri from picasso?

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.

Categories

Resources