简体   繁体   中英

android - sharing image inside imageview that gets changed with viewpager

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM