youness youness - 1 year ago 53
Android Question

Android ShareActionProvider with ViewPager forces me save Image

I have a big issue with

. My application is like a gallery application using
, that keeps track of favorite image items, and gives possibility to share them through
Everything works fine except that whichever image I scroll to is saved in the directory I use to get Uri.

Here is my code

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(, menu);
mMenu = menu;
favoriteItem = mMenu.findItem(;

MenuItem item = (MenuItem) mMenu.findItem(;

mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);


private void initShareIntent() {
if (mShareActionProvider != null) {
Uri bmpUri = getLocalBitmapUri(mCurrentImage);
Intent shareIntent = new Intent();
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");


public Uri getLocalBitmapUri(ImageView imageView) {
if(imageView != null){
Drawable mDrawable = imageView.getDrawable();
Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();

String path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),
mBitmap, "Image Description", null);

Uri uri = Uri.parse(path);
return uri;
return null;

I need to call


each time I instatiate the view in the ViewPager

class GalleryViewPagerAdapter extends PagerAdapter {

public int getCount() {
return listeOfPhotos.size();

public View instantiateItem(ViewGroup container, int position) {
mPhoto = listeOfPhotos.get(position);

.displayImage(mPhoto.getUrl(), mCurrentImage, FragmentGallery.options, null, null);

container.addView(mCurrentImage, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

//Change the Favorite Icon and Invalidate Menu

return mCurrentView;


I would be glad if you guys help me with this problem.

Thank you

Answer Source

You call initShareIntent which calls eventually getLocalBitmapUri in every Fragment of yours. That is the one that creates a separate image in the gallery.

I would suggest you that to stop using ShareActionProvider. Google stopped using it in their applications. With ShareActionProvider you have know the URI and the sharing Intent before user want to share it and clicks the share button. So it becomes a little bit hard to do.

You can checkout the below links for how to do that. You basically have to generate the sharing Intent when the button is clicked instead of in onCreate callback.

If you use normal ActionBar button and fire sharing dialog yourself, you can call and create the local Bitmap using getLocalBitmapUri only when the user wants to share the image.

If you must use ShareActionProvider, you shouldn't recreate a new local Bitmap. Even when you show an online image, you cache it somewhere right. A local copy of the image is already out there somewhere. You should find it and give it to the ShareActionProvider as an URI. Of course, it should be publicly accessible. So you can set your image cache folder somewhere in public folders like sdcard etc.