Android Studio Share button

20,928

Solution 1

Use share intent http://developer.android.com/training/sharing/send.html

sample code

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

Solution 2

You can send content by invoking an implicit intent with ACTION_SEND.

To send images or binary data:

final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
final File photoFile = new File(getFilesDir(), "foo.jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
startActivity(Intent.createChooser(shareIntent, "Share image using"));

send an image along with text. This can be done with:

String text = "Look at my awesome picture";
Uri pictureUri = Uri.parse("file://my_picture");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share images..."));

Sharing multiple images can be done with:

Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
Share:
20,928
Ryan
Author by

Ryan

I'm not a very good programmer (I'm actually an incredibly horrible one) and I will probably only use it in my studies. However it interests me and I would like to have a good understanding of how it works with my everyday life. Currently studying CSIS to become.... Something... If you come across me, I am utterly sorry you have to see such garbage code...

Updated on January 05, 2021

Comments

  • Ryan
    Ryan over 3 years

    I wanted to make a share button on a Navigation drawer, when the user touches the button it will open up that black drawer with the list of all applications and the user can share the apps Google play link. Is there any generic code template? the only answers I have found is to just share it on one application such as Facebook which seems useless because not everyone uses Facebook.