0

I have a screen where images are swiped left and right using imageview and viewpager. It picks up images that are specified in the code (drawables in the project). I was wondering what would be the best way for the user to shars the images.

Would it be better to have a button that saves each image to card and shares it, or have a button that screenshots whatever is on the screen and shares it? I tried searching but found that the screenshot thing only works on rooted phones (which wouldn't work for my app) but those threads were from 2 years ago, so maybe things have changed RE: screenshotting programataically?

I'm not looking for code snippets, just want to know what the best strategy for this would be, without changing too much how my images are viewed.

  • [This looks like a really good place to start](https://developer.android.com/training/sharing/shareaction.html) and follows the expected behaviour when sharing images. I assume you would need to work out the apparatus for sharing the drawable, but the guide looks to be solid. – CodeMonkey Nov 04 '14 at 22:20
  • [Here's the bit about sending an image](https://developer.android.com/training/sharing/send.html#send-binary-content) which I think is directly related. I would steer clear of screenshotting, as it will save the image in a folder on the user's device, which is just doubling up the space used. – CodeMonkey Nov 04 '14 at 22:23
  • You don't need to take a screenshot. You already have a reference to the image, just have a share button in the ActionBar and share the image along with any other text if you would like. You just create and Intent, attach the data to it and then you send it. – Dimitar Darazhanski Nov 04 '14 at 22:27

1 Answers1

0

According to the documentation, you want to look at creating a share intent as follows:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

You can then add an easy share action to your action bar which is what the user expects when browsing shareable images.

Create a menu resource:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
        android:id="@+id/menu_item_share"
        android:showAsAction="ifRoom"
        android:title="Share"
        android:actionProviderClass=
            "android.widget.ShareActionProvider" />
...
</menu>

Then inflate it in your onCreate and assign it some functionality using the Intent you created earlier:

private ShareActionProvider mShareActionProvider;
...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.share_menu, menu);

// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);

// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();

// Return true to display menu
    return true;
}

// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
    mShareActionProvider.setShareIntent(shareIntent);
    }
}

I would steer away from screenshotting the image and creating a copy of data you already have available.

CodeMonkey
  • 1,426
  • 1
  • 14
  • 32
  • I was under the impression you can't share an image that's in the program and not on the user's SD card, or has this changed? –  Nov 06 '14 at 17:25
  • I wasn't aware of that, I have found some answers on SO [(such as this one)](http://stackoverflow.com/a/7177103/716588) which seem to point towards using a custom `ContentProvider` to access images from your `Assets` folder. Have you had a look at those? I will update my answer if it is a working solution (I have not tried it this way myself). – CodeMonkey Nov 11 '14 at 05:28