Anthony
Anthony

Reputation: 7828

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!

Upvotes: 1

Views: 432

Answers (2)

Anthony
Anthony

Reputation: 7828

I revisited this problem and implemented the ContentProvider answer above. It is by far a superior solution.

I came up with a rather interesting solution. I used onShareTargetSelected and returned true to indicate that I was handling the intent.

This caused a minor problem since ShareActionProvider won't store historical data (place the favorite app at the top of the list) if you handle the intent.

Since I'm using ActionBarSherlock I was able to modify ActivityChooserModel to store the historical data regardless of whether the intent is handled with custom code in onShareTargetSelected.

Finally I added a couple boolean checks that manage if a share intent is pending to ask the user if they want to wait for it to complete before changing activities, or cancel it.

I'm not entirely sure what I've done is kosher yet and it's pretty complicated, so I don't want to post code, but feel free to contact me for more info.

Upvotes: 0

farindk
farindk

Reputation: 260

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.

Upvotes: 2

Related Questions