Search code examples
androidandroid-actionbaractionbarsherlockshare

ShareActionProvider with background processing


I have a gallery application that handles more image formats then Android natively. I'd like to add a share option that will allow the user to share any selected image(s) as jpeg. I'd prefer to use the ShareActionProvider since it integrates nicely with the UI.

Here's the problem. To output jpegs requires a background process that will convert the images. I can't simply convert every time a user adds a selection since it would bog the app and they may not want to share in the end. As far as I've seen there is no way to intercept the ShareProvider once the action has been clicked. At that point it appears it must be populated with any necessary URIs.

Has anyone come up with a way of handling necessary background processing before a ShareActionProvider is executed?

I've exhausted many ideas:

  1. onShareTargetSelected(ShareActionProvider, Intent), but this does not allow any modification of the intent, not even the URI bundle.
  2. custom Intent that fires an AsyncTask then the share intent, but that can't access the user selected handler.

At this point I'm fairly certain I'll need to implement a classical action item with a share chooser, but I wanted to see if anyone has come up with a clever solution to this. Thanks guys!


Solution

  • The URI you provide to the ShareActionProvider can also refer to a custom ContentProvider. You simply encode all the conversion parameters into the URI and override the openFile() method in the ContentProvider. In this way, you postpone the conversion until the user actually shares.

    Using any background process seems to be unnecessary with this solution.