Search code examples
androidimageshareactionprovider

Android ShareActionProvider Image Problems


I am trying to send an image from an app that I am developing, I have the following code but am stuck as to how to create a Uri from a file that is in my /drawable folder, any ideas? (Very confused at this point)

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.second, menu);
    MenuItem shareitem = menu.findItem(R.id.menu_item_share);
    myShareActionProvider = (ShareActionProvider) shareitem.getActionProvider();
    myShareActionProvider.setShareIntent(createShareIntent());

    ActionBar actionBar = getActionBar();
    actionBar.show();
    return true;

}

public Intent createShareIntent() {
    image = ???
    Uri uri = Uri.fromFile(image);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/png");
    return shareIntent;
}

Solution

  • private Intent createShareIntent() {
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("image/*");
            Uri uri = Uri.parse("android.resource://your.package.name/" + R.drawable.image);
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            return shareIntent;
        }