I'm trying to share some text using an intent:
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_TEXT, "TEXT");
and warping with chooser:
startActivity(Intent.createChooser(sms, getResources().getString(R.string.share_using)));
it works! but only for email app.
what I need is a general intent for all messaging app: emails, sms, IM (Whatsapp, Viber, Gmail, SMS...)
tried using android.content.Intent.ACTION_VIEW
and tried using i.setType("vnd.android-dir/mms-sms"); nothing helped...
("vnd.android-dir/mms-sms" shared using sms only!)
Use the code as:
/*Create an ACTION_SEND Intent*/
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
/*This will be the actual content you wish you share.*/
String shareBody = "Here is the share content body";
/*The type of the content is text, obviously.*/
intent.setType("text/plain");
/*Applying information Subject and Body.*/
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.share_subject));
intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
/*Fire!*/
startActivity(Intent.createChooser(intent, getString(R.string.share_using)));
New way of doing this would be using ShareCompat.IntentBuilder like so:
// Create and fire off our Intent in one fell swoop
ShareCompat.IntentBuilder
// getActivity() or activity field if within Fragment
.from(this)
// The text that will be shared
.setText(textToShare)
// most general text sharing MIME type
.setType("text/plain")
.setStream(uriToContentThatMatchesTheArgumentOfSetType)
/*
* [OPTIONAL] Designate a URI to share. Your type that
* is set above will have to match the type of data
* that your designating with this URI. Not sure
* exactly what happens if you don't do that, but
* let's not find out.
*
* For example, to share an image, you'd do the following:
* File imageFile = ...;
* Uri uriToImage = ...; // Convert the File to URI
* Intent shareImage = ShareCompat.IntentBuilder.from(activity)
* .setType("image/png")
* .setStream(uriToImage)
* .getIntent();
*/
.setEmailTo(arrayOfStringEmailAddresses)
.setEmailTo(singleStringEmailAddress)
/*
* [OPTIONAL] Designate the email recipients as an array
* of Strings or a single String
*/
.setEmailTo(arrayOfStringEmailAddresses)
.setEmailTo(singleStringEmailAddress)
/*
* [OPTIONAL] Designate the email addresses that will be
* BCC'd on an email as an array of Strings or a single String
*/
.addEmailBcc(arrayOfStringEmailAddresses)
.addEmailBcc(singleStringEmailAddress)
/*
* The title of the chooser that the system will show
* to allow the user to select an app
*/
.setChooserTitle(yourChooserTitle)
.startChooser();
If you have any more questions about using ShareCompat, I highly recommend this great article from Ian Lake, an Android Developer Advocate at Google, for a more complete breakdown of the API. As you'll notice, I borrowed some of this example from that article.
If that article doesn't answer all of your questions, there is always the Javadoc itself for ShareCompat.IntentBuilder on the Android Developers website. I added more to this example of the API's usage on the basis of clemantiano's comment.
This a great example about share with Intents in Android:
* Share with Intents in Android
//Share text:
Intent intent2 = new Intent(); intent2.setAction(Intent.ACTION_SEND);
intent2.setType("text/plain");
intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" );
startActivity(Intent.createChooser(intent2, "Share via"));
//via Email:
Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_SEND);
intent2.setType("message/rfc822");
intent2.putExtra(Intent.EXTRA_EMAIL, new String[]{EMAIL1, EMAIL2});
intent2.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" );
startActivity(intent2);
//Share Files:
//Image:
boolean isPNG = (path.toLowerCase().endsWith(".png")) ? true : false;
Intent i = new Intent(Intent.ACTION_SEND);
//Set type of file
if(isPNG)
{
i.setType("image/png");//With png image file or set "image/*" type
}
else
{
i.setType("image/jpeg");
}
Uri imgUri = Uri.fromFile(new File(path));//Absolute Path of image
i.putExtra(Intent.EXTRA_STREAM, imgUri);//Uri of image
startActivity(Intent.createChooser(i, "Share via"));
break;
//APK:
File f = new File(path1);
if(f.exists())
{
Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_SEND);
intent2.setType("application/vnd.android.package-archive");//APk file type
intent2.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f) );
startActivity(Intent.createChooser(intent2, "Share via"));
}
break;
Use below method, just pass the subject and body as arguments of the
method
public static void shareText(String subject,String body) {
Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
txtIntent .setType("text/plain");
txtIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
txtIntent .putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(txtIntent ,"Share"));
}
Below is the code that works with both the email or messaging app.
If you share through email then the subject and body both are added.
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareString = Html.fromHtml("Medicine Name:" + medicine_name +
"<p>Store Name:" + “store_name “+ "</p>" +
"<p>Store Address:" + “store_address” + "</p>") .toString();
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Medicine Enquiry");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareString);
if (sharingIntent.resolveActivity(context.getPackageManager()) != null)
context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
else {
Toast.makeText(context, "No app found on your phone which can perform this action", Toast.LENGTH_SHORT).show();
}
By Creating an Intent using ACTION_SEND you will be able to put extra its type is Intent.EXTRA_TEXT, the second argument is the text you want to share.
Then by setting the share type as text/plain, the Intent service will bring you all apps that support sharing text
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);
Images or binary data:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpg");
Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
or HTML:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text shared.</p>"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));
Kotlin
Inside the click listener needed to add this module for sharing text via applications like whatsApp, email like Gmail, Slack..
shareOptionClicked.setOnClickListener{
val shareText = Intent(Intent.ACTION_SEND)
shareText.type = "text/plain"
val dataToShare = "Message from my application"
shareText.putExtra(Intent.EXTRA_SUBJECT, "Subject from my application")
shareText.putExtra(Intent.EXTRA_TEXT, dataToShare)
startActivity(Intent.createChooser(shareText, "Share Via"))
}
This code is for sharing through sms
String smsBody="Sms Body";
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", smsBody);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
100 % Working Code For Gmail Share
Intent intent = new Intent (Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"anyMail#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Any subject if you want");
intent.setPackage("com.google.android.gm");
if (intent.resolveActivity(getPackageManager())!=null)
startActivity(intent);
else
Toast.makeText(this,"Gmail App is not installed",Toast.LENGTH_SHORT).show();
Related
For my application, I would like to give share option.
For this, I have edit text to give phone number and a share button. If user gives a valid number and clicks share button, list of all available message sending application should list. If user selects an application from the list then a message with the application link should send his phone using the selected application.
How can I implement this? Please help.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "message link");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Chooser title text"));
try this
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,"your application link"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));
Try Below code,This is exactly that you want ,
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, Your_EditText_object.getText().toString());
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Your Title"));
Try this:
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(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
References link:
https://developer.android.com/training/sharing/send.html
public static void callShare(Context theCtx, String theImagePath, String theText)
{
// Pass the message and Image path that you want to share
File myImageFile = new File(theImagePath);
String shareBody = theText; //"Here is the share content body " ;
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
if (myImageFile.exists()) {
// email address is required to be filled.
sharingIntent.setType("image/jpeg");
// "file://" and .getAbsolutePath is very important for Extra_Stream.
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + myImageFile.getAbsolutePath()));
} else if (!theText.isEmpty()) {
sharingIntent.setType("text/*");
}
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, ""); //"Subject here"
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
sharingIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
theCtx.startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
There are two requirements:
Email with attachment
In the Intent chooser, there should be only Email apps.
What I have known/done:
Intent.ACTION_SENDTO with intent.setData(Uri.parse("mailto:")) can make sure that there are only Email apps in Intent chooser but it will not bring attachment(For some apps like Gmail it will, but there are also many apps that will ignore attachment).
Intent.ACTION_SEND can send Email with attachment. However, in Intent chooser, there will be apps that are actually not Email apps but can response to Intent.ACTION_SEND. Using intent.setType("message/rfc822") can reduce number of those apps but not all.
References this answer: https://stackoverflow.com/a/8550043/3952691 and nearly succeed in my goals. My code is as below:
private static void sendFeedbackWithAttachment(Context context, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
if (resolveInfos.isEmpty()) {
Toast.makeText(context, context.getString(R.string.error_activity_not_found),
Toast.LENGTH_SHORT).show();
} else {
// ACTION_SEND may be replied by some apps that are not email apps. However,
// ACTION_SENDTO doesn't allow us to choose attachment. As a result, we use
// an ACTION_SENDTO intent with email data to filter email apps and then send
// email with attachment by ACTION_SEND.
List<LabeledIntent> intents = new ArrayList<>();
Uri uri = getLatestLogUri();
for (ResolveInfo info : resolveInfos) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage(info.activityInfo.packageName);
i.setClassName(info.activityInfo.packageName, info.activityInfo.name);
i.putExtra(Intent.EXTRA_EMAIL, new String[] { Def.Meta.FEEDBACK_EMAIL });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_STREAM, uri);
intents.add(new LabeledIntent(i, info.activityInfo.packageName,
info.loadLabel(context.getPackageManager()), info.icon));
}
Intent chooser = Intent.createChooser(intents.remove(0),
context.getString(R.string.send_feedback_to_developer));
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,
intents.toArray(new LabeledIntent[intents.size()]));
context.startActivity(chooser);
}
}
However, on some devices(For example, Xiaomi 2S with MIUI V5, I don't know if this can be influenced by a third-party rom), the result is an empty Intent chooser. And it seems that above Android 6.0, Intent.EXTRA_INITIAL_INTENTS has some bugs(Custom intent-chooser - why on Android 6 does it show empty cells?, and another one: https://code.google.com/p/android/issues/detail?id=202693).
As a result, I don't know how to achieve my goals. Please help me, thank you in advance.
Try the below code to Send a mail
String filename="filename.vcf";
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd#gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
There are two ways to do this
OPTION 1
Intent emailIntent = new Intent(
android.content.Intent.ACTION_VIEW);
//Option 1
Uri data = Uri.parse("mailto:?subject=" + "blah blah subject"
+ "&body=" + "blah blah body" + "&to=" + "sendme#me.com");
emailIntent.setData(data);
startActivity(Intent.createChooser(emailIntent, ""));
Result
OPTION 2
It works perfactly except it wont filter out FTP
//Option 2
emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
final String[] toRecipients = new String[] { "sendme#me.com", "", };
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, toRecipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "blah blah subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
Html.fromHtml("blah blah body"));
startActivity(Intent.createChooser(emailIntent, ""));
Result
Both ways have minor flaws I show you both ways it is now upto you to pick one.
I am experiecing "This action is not currrently supported" error condition when execute the following code snippet in Android 2.1. What is wrong with the snippet?
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
Uri uri = Uri.parse("mailto:myemail#gmail.com");
intent.setData(uri);
intent.putExtra("subject", "my subject");
intent.putExtra("body", "my message");
startActivity(intent);
}
If you use ACTION_SENDTO, putExtra() does not work to add subject and text to the intent. Use setData() and the Uri tool add subject and text.
This example works for me:
// ACTION_SENDTO filters for email apps (discard bluetooth and others)
String uriText =
"mailto:youremail#gmail.com" +
"?subject=" + Uri.encode("some subject text here") +
"&body=" + Uri.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send email"));
Actually I found a way where the EXTRAs work.
This is a combination of an answer I found here regarding ACTION_SENDTO
http://www.coderanch.com/t/520651/Android/Mobile/no-application-perform-action-when
and something for HTML which I found here:
How to send HTML email
(but the variant of how to use ACTION_SENDTO in this HTML post didn't work for me - I got the "action not supported" which you are seeing)
// works with blank mailId - user will have to fill in To: field
String mailId="";
// or can work with pre-filled-in To: field
// String mailId="yourmail#gmail.com";
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
Uri.fromParts("mailto",mailId, null));
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject text here");
// you can use simple text like this
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Body text here");
// or get fancy with HTML like this
emailIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
.append("<p><b>Some Content</b></p>")
.append("<a>http://www.google.com</a>")
.append("<small><p>More content</p></small>")
.toString())
);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
You Can Try This One
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", emailID, null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT,
Subject);
emailIntent.putExtra(Intent.EXTRA_TEXT,
Text);
startActivity(Intent.createChooser(emailIntent, Choosertitle);
It Works For me
Is there any way to customize the createChooser of share intent in startActivity(Intent.createChooser(i,"Share via"));?
Showing the apps not in a dialogue box maybe in scroll view as buttons
String urlToShare = "www.google.com"
code to share link twitter
try {
Intent shareIntent = ShareCompat.IntentBuilder.from(getParent())
.setType("text/plain")
.setText("Sharing text with image link \n "+urlToShare).setStream(null)
.getIntent()
.setPackage("com.twitter.android");
startActivity(shareIntent);
} catch (Exception e) {
// If we failed (not native FB app installed), try share through SEND
Intent intent = new Intent(Intent.ACTION_SEND);
String sharerUrl = "https://twitter.com/intent/tweet?text=" + urlToShare;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(intent);
}
code to share link google plus
try {
Intent shareIntent = ShareCompat.IntentBuilder.from(getParent())
.setType("text/plain")
.setText("Sharing text with image link \n "+urlToShare).setStream(null)
.getIntent()
.setPackage("com.google.android.apps.plus");
startActivity(shareIntent);
} catch (Exception e) {
// If we failed (not native FB app installed), try share through SEND
Intent intent = new Intent(Intent.ACTION_SEND);
String sharerUrl = "https://plus.google.com/share?url=" + urlToShare;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(intent);
}
code to share link whatsapp
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
intent.setType("text/plain");
intent.setPackage("com.whatsapp");
startActivity(intent);
Yes you can do it by getting the share options as activities and pass them to your adapter, i am posting a sample code
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List activities = this.getPackageManager().queryIntentActivities(sendIntent, 0);
By this you will get share Intents in activities object and then you can convert it to Object Array some thing like this
Objects[] item = activities.toArray();
for( int i=0; i<item.length; i++ ) {
ResolveInfo infoName = (ResolveInfo) items[i];
String name = info.activityInfo.name;
Drawable logo = info.loadIcon(context.getPackageManager());
// Set them to your Views
}
and when your view is clicked and you want to perform the share functionality you will do some thing like this
ResolveInfo info = (ResolveInfo) item(position);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Your Text");
startActivity(intent);
this is what i have done to share link and image to another apps something like this just try
/*
* Method to share either text or URL.
*/
private void shareTextUrl() {
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide
// what to do with it.
share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
share.putExtra(Intent.EXTRA_TEXT, "http://www.google.com");
startActivity(Intent.createChooser(share, "Share link!"));
}
/*
* Method to share any image.
*/
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
I have a textbox(EditText) in my app & a button, what i want to do is that when anyone tap the button, the text written in the textbox(EditText) get copied & this text can be shared to any of the app such as - Messaging, Gmail, Ymail, etc.
Now What i am doing is getting the text from the "EditText" & storing it into a new variable (string) say 'a' & now applying the Intent "ACTION_SEND_MULTIPLE"
Here is the CODE for Intent
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"example#gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "a");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, a);
startActivity(Intent.createChooser(emailIntent, "Share it via..."));
I'm not sure what your problem is, but this is how you get text from an editText
String mString= et.getText().toString();
Then put it in the share intent
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, mString);
shareIntent.setType("text/plain");
startActivity(shareIntent);
If you want to send it as an email only, and only allow email clients to respond to the intent, it goes something like this.
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode("mail to address")
+ "?subject=" + Uri.encode("subject here")
+ "&body=" + Uri.encode("body here");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send..."));
This allows you to enter the subject field & mailto field...etc
You need "createChooser" maybe:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, edittext.getText().toString());
startActivity(Intent.createChooser(intent, "chooser title"));