使用ViewPager的Android ShareActionProvider强制我保存图像

时间:2015-04-07 06:42:24

标签: android image android-viewpager shareactionprovider

ShareActionProvider我遇到了很大的问题。我的应用程序就像使用ViewPager的图库应用程序一样,可以跟踪喜欢的图像项目,并可以通过ShareActionProvider分享它们。 一切正常,但我滚动到的图像保存在我用来获取Uri的目录中。

这是我的代码

@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;
}

我需要致电

getActivity().invalidateOptionsMenu();

每次我在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;
    }

}

如果你们帮我解决这个问题,我会很高兴的。

谢谢

2 个答案:

答案 0 :(得分:0)

您致电initShareIntent,在您getLocalBitmapUri的每个Fragment中呼叫ShareActionProvider。这是在图库中创建单独图像的那个。

我建议您停止使用ShareActionProvider。谷歌停止在他们的应用程序中使用它使用onCreate,您知道URI和共享意图,然后用户想要共享它并单击共享按钮。所以它变得有点难。

您可以查看以下链接,了解如何执行此操作。您只需在单击按钮而不是Bitmap回调时生成共享Intent。 http://developer.android.com/training/sharing/send.html#send-binary-content https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents

如果您自己使用普通的ActionBar按钮和火灾共享对话框,则只有在用户想要共享图像时,才能使用getLocalBitmapUri呼叫并创建本地ShareActionProvider

如果您必须使用Bitmap,则不应重新创建新的本地ShareActionProvider。即使您显示在线图像,也可以将其缓存到正确的位置。图像的本地副本已经存在于某处。您应该找到它并将其作为URI提供给{{1}}。当然,它应该是公开的。因此,您可以将图像缓存文件夹设置在公共文件夹中的某个位置,如SD卡等。

答案 1 :(得分:0)

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.