androidimageandroid-viewpagershareactionprovider

Android ShareActionProvider with ViewPager forces me save Image


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

Here is my code

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
    inflater.inflate(R.menu.menu_fragement_gallery, menu);
    mMenu = menu;
    favoriteItem = mMenu.findItem(R.id.idItemFavoris);

    MenuItem item = (MenuItem) mMenu.findItem(R.id.idItemShashare);

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

    initShareIntent();
}

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

        shareIntent.setType("image/*");

        mShareActionProvider.setShareIntent(shareIntent);
    }
}
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

getActivity().invalidateOptionsMenu();

each time I instatiate the view in the ViewPager

class GalleryViewPagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return listeOfPhotos.size();
    }

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

        ImageLoader.getInstance()
                .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
        initIconFavorite();

        return mCurrentView;
    }

}

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

Thank you


Solution

  • 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. http://developer.android.com/training/sharing/send.html#send-binary-content https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents

    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.