Question

I'm currently using Picasso and OkHttp for my bitmap caching. What I want to do is retrieve a specific cached bitmap, draw on it, and then put that drawn on bitmap back into the memory and disk cache. My implementation is pretty standard, I'm just using Target and adding the loaded bitmap to a custom ImageView which resides in a GridView. I've been looking around and I can't seem to find a way to do this. Do these libraries even support something like this?

Was it helpful?

Solution

Picasso is just the library for downloading caching and displaying the image (with perks like transformations, cross fading etc) not for editing. What you seem to do seems to go beyond the scope of Picasso usage.

I'd say you can still use Picasso for downloading the image if you want to, but when you get a Bitmap it's your app that has do store it's modified version. So when a user finishes drawing it you must store it locally into a file yourself (if it's a big image you can also create separate smaller thumbnails for GridView) and use this version. To save changed Bitmap contents to a file you can call

outBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);

You can load local files into your GridView using Picasso too: Picasso.with(context).load("fileUrl").into(mImageView);. So in your code you would check if you have local edited version and load it from local file using Picasso or load it from server using Picasso otherwise.

If you want to save image on the server you can send it to server after user has edited it. Then Picasso+OkHttp will download it again (assuming you updated http cache attributes like ETag on server).

OTHER TIPS

Try using a picasso transformation.

picasso's workflow is like this:

  1. get the image from network
  2. cache the raw image in memory/cache
  3. apply optional transformation(s) and cache those results too

So in your case you would need to make an custom transformation that alters your bitmap. The second time you request the bitmap with this specific transformation, it will be loaded from cache.

public class YourTransformation implements com.squareup.picasso.Transformation {

    @Override
    public Bitmap transform ( final Bitmap source ) {
        final Paint paint = new Paint();

        Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        // DO YOUR DRAWING STUFF ON CANVAS

        if (source != output) {
            source.recycle();
        }

        return output;
     }

     @Override
     public String key () {
         return "customTransformation" + SOME_UNIQUE_IDENTIFIER 
     }
}

Don't forget to override the key() method as picasso uses it for generating the cache entry.

In your code you can simply use it with your normal picasso request:

picasso.load(url).transform(new YourTransformation()).into(imageView);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top