Sending mail with attachmen via mail without chooser - android

I am sending mail via my app with this code:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email#example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
intent.putExtra(Intent.EXTRA_TEXT, "body text");
Uri uri = Uri.parse("file://" + file_name+".jpg");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Send email..."));
This works just fine, but how can I do this without Intent.createChooser and do not let the user choose app for sharing and go directly to Gmail app, assuming each android phone have it.

You can used the following code to open whatever intent you want eg gmail, facebook, email etc..Simple in the type as used in code pass "gmail" if you want to open gmail, pass "face" if u want to open facebook
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/html");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
if (!resInfo.isEmpty())
{
for (ResolveInfo info : resInfo)
{
if (info.activityInfo.packageName.toLowerCase().contains(type) || info.activityInfo.name.toLowerCase().contains(type))
{
intent.putExtra(android.content.Intent.EXTRA_TEXT, htmlBody);
intent.setPackage(info.activityInfo.packageName);
startActivity(Intent.createChooser(intent, getResources().getString(R.string.share_send_text)));
}
}

Related

Search for linkedin app in android via intent

I am using intent to get the list of all apps i can post text to. However, linkedin is not appearing in that list. Do i need to do anything extra for linkedin?
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "dcsd");
package= mContext.getPackageManager();
List<ResolveInfo> appTargets= package.queryIntentActivities(shareIntent, 0);
I am able to get all other apps like Facebook, Twitter except LinkedIn?
What could be the possible reason?
Code is
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String shareBody = "https://developers.facebook.com/docs/android/share";
intent.putExtra(Intent.EXTRA_TITLE,"Share From Test App");
intent.putExtra(Intent.EXTRA_TEXT,shareBody);
intent.putExtra(Intent.EXTRA_SUBJECT, "hello");
startActivity(Intent.createChooser(intent, "Share With"));
Add this to the intent:
intent.putExtra(Intent.EXTRA_SUBJECT, "dsvs");

How to send Email with attachment using only email apps?

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.

Sharing text from app to Email app in android

I have the following code which used to send text from my app to Email:
Intent mail = new Intent(Intent.ACTION_VIEW);
mail.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
mail.putExtra(Intent.EXTRA_EMAIL, new String[] { });
mail.setData(Uri.parse(""));
mail.putExtra(Intent.EXTRA_SUBJECT, "Country Decryption");
mail.setType("plain/text");
mail.putExtra(Intent.EXTRA_TEXT, "my text");
ctx.startActivity(mail);
It works , but as you see, it uses Gmail app, how do I make it use Email application instead of Gmail?
I mean this app:
and what about sharing to Facebook ? I found that Facebook does not support sharing using intent anymore, and I have to use Facebook SDK, but I couldn't find any simple procedure to do that. Is there any simple way?
Regards.
well to use other email apps am afraid you would have to create a chooser dialog and let the user choose which app to use, something like this
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc#mail.com", null));
emailIntent.putExtra(Intent.EXTRA_EMAIL, "address");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send Email..."));
You have to create a specific filter on your ACTION_SEND and you can read a complete answer here.
This is the code in which you can choose which app show in the app chooser
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if(packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
} else if(packageName.contains("facebook")) {
// Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
// One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
// will show the <meta content ="..."> text from that page with our link in Facebook.
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
} else if(packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
} else if(packageName.contains("android.gm")) { // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
intent.setType("message/rfc822");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
This works to intent just the gmail app.
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.setPackage("com.google.android.gm");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(sharingIntent);

Intent.ACTION_SENDTO show two options , I want to show only one

Intent.ACTION_SENDTO in shows two options but my clent is asking to remove the gmail option and i don't see a way out please help me
Intent emailIntent =
new Intent(Intent.ACTION_SENDTO,
Uri.fromParts( "mailto",userInput.getText().toString(), null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Press Release");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Please view this press release");
startActivity(Intent.createChooser(emailIntent,"Send mail using..."));
Use
emailIntent.setPackage(PackageName of Email app); before calling startActivity.
You need to set email client package name but, in Samsung devices com.sec.android.email is the default In-Built Mail client, but in HTC it is com.htc.android.mail and so on. So first you need to filter that application and then set to intent. I am adding the solution
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", userInput.getText().toString(), null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Press Release");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"Please view this press release");
// Identify the package name of email client and set to intent
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(emailIntent, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(".android.email")
|| info.activityInfo.name.toLowerCase().contains(".android.email")) {
emailIntent.setPackage(info.activityInfo.packageName);
// And now call
startActivity(Intent.createChooser(emailIntent, "Send mail using..."));
}
}
}
You should read Android: How to get the native Email clients package name
IF YOU WANT TO REMOVE GMAIL CLIENT FROM LIST create a custom cooser
List<Intent> intents = new ArrayList<Intent>();
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(sendIntent, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent neededShareIntent = new Intent(android.content.Intent.ACTION_SEND);
neededShareIntent.setType("text/plain");
neededShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
neededShareIntent.setPackage(packageName);
if (!StringUtils.equals(packageName, "com.google.android.gm")){
intents.add(neededShareIntent);
}
}
Intent chooserIntent = Intent.createChooser(intents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
Pls test this code and check

Android - How to open the email client directly

I want to open the default email client instead of showing the options. I tried but i am not getting please can anyone help me.
I used the following code:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Allergy Journal");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<small>"+sb.toString()+"</small>"));
startActivity(Intent.createChooser(emailIntent, "Email:"));
It's show the options
But I want to open then Default email client directly.
Frame a String in the format String URI="mailto:?subject=" + subject + "&body=" + body;
and
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse(URI);
intent.setData(data);
startActivity(intent);
This will open up the default e-mail program selected by the user.
Linkify does it this way. Check out it's source code, if you like.
You can used the following code to open whatever intent you want eg gmail, facebook, email etc..Simple in the type as used in my code pass "gmail" if you want to open gmail, pass "face" if u want to open facebook
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/html");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
if (!resInfo.isEmpty())
{
for (ResolveInfo info : resInfo)
{
if (info.activityInfo.packageName.toLowerCase().contains(type) || info.activityInfo.name.toLowerCase().contains(type))
{
intent.putExtra(android.content.Intent.EXTRA_TEXT, htmlBody);
intent.setPackage(info.activityInfo.packageName);
startActivity(Intent.createChooser(intent, getResources().getString(R.string.share_send_text)));
}
}

Categories

Resources