Picasso - get the path of the uploaded image

I am using Picasso in my Android application to download images from a URL. These images are displayed in some ListView . When a user clicks on an item in this ListView , I need to share an Intent with the selected image.

I create a collaborative intent using this code:

 Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(localPath)); shareIntent.putExtra(Intent.EXTRA_TEXT, mUrl); shareIntent.setType("image/jpg"); startActivity(Intent.createChooser(shareIntent, getString(R.string.label_share))); 

In the above code there is a local variable localPath . This variable must contain the path to the image. I believe that Picasso has a cache where uploaded images are uploaded. Is there any way to get the path to the selected image?

UPDATE: partial solution

Finally, I found a solution that works for me. But I do not know if this is the right decision or if a better solution exists.

I created a class that implements the com.squareup.picasso.Target interface, and in the onBitmapLoaded(Bitmap bitmap, LoadedFrom from) method onBitmapLoaded(Bitmap bitmap, LoadedFrom from) I presented this implementation:

 @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { mImageView.setImageBitmap(bitmap); mImageUri = getBitmapUri(bitmap, mName)); } private Uri getBitmapUri(Bitmap bitmap, String title) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = MediaStore.Images.Media.insertImage(mContext.getContentResolver(), bitmap, title, null); return Uri.parse(path); } 

After that, I have mImageUri , which I can pass for sharing. Perhaps this piece of code will be useful to someone.

But I do not know if a better solution exists. In my solution, I have to store Bitmap on disk, but maybe the same image already exists on disk because Picasso downloaded it. In addition, I do not know if I need to delete an image from MediaStore when I no longer need it.

+5
source share

All Articles