android - Share html link to facebook, twitter or email - android

I am trying to share an html link to either facebook, twitter or email. Here is what I have so far, but two things are going wrong.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(Intent.EXTRA_TEXT, "<!DOCTYPE html><html><body>" + htmlUrl + "</body></html>");
startActivity(Intent.createChooser(shareIntent, "Share!"));
WHERE htmlUrl = "<a href=\"http://{url}/?q=" + queryString.substring(0, queryString.length() - 1) + "\" >Text to url! </a>"
First this only shows the email application in the list.
Secondly it's showing up as full text within the email and not as an HTML item.
Thank,
Dman

Your MIME type is wrong. Use this instead:
shareIntent.setType("text/plain");
If you just want to share a link and not full-blown HTML, just use the url as the Intent.EXTRA_TEXT value:
shareIntent.putExtra( Intent.EXTRA_TEXT, url );
Note that only a few apps (like GMail, Bluetooth and Dropbox) support sharing HTML. Use plain text to allow more apps to catch your Intent

You only miss a small change to your code - use Html.fromHtml to encode the string as HTML.
a more detailed example is available at this link:
http://blog.iangclifton.com/2010/05/17/sending-html-email-with-android-intent/
This will not allow you to share on Facebook since the Facebook app dows not support ACTION_SEND with text/html but this will allow you to share HTML content using the Gmail app

Share to Facebook (using this method can only share the link)
Intent facebookIntent = new Intent(Intent.ACTION_SEND);
facebookIntent.setType("text/plain");
facebookIntent.setPackage("com.facebook.katana");
facebookIntent.putExtra(Intent.EXTRA_TEXT, shareUrl);
startActivity(facebookIntent);
Share to Email (using this method can change the email content to html format, but don't work using default email client, it works on gmail client.)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_SUBJECT, shareTitle);
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(content of your email));
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Share to Email..."));
Share to Twitter
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, your content);
intent.setType("application/twitter");
startActivity(intent);
Hope this helps.

This
shareIntent.setType("text/html");
should do for the html part. Why only the email shows up no idea, do you have other apps like facebook, twitter etc installed to handle share intents?

Related

Send HTML e-mail with Intent

I want to send a HTML e-mail when an user decides to share my app. I'm using HTML in order to have a customised and appellative message.
First approach, I tried to create a String with my HTML (inline style) using Html.fromHtml but when I received the e-mail it was pure txt, no customization.
Second approach, send a HTML file attached. The problem with this approach is that the HTML is not showed until the user opens the attach.
What's the best solution, is it possible?
Thanks!
You can achieve your task using this method Html.fromHtml(String);
Html.fromHtml("<font color='#ff123456'>text</font>")
You can pass Spanned text in your extra. To ensure that the intent resolves only to activities that handle email (e.g. Gmail and Email apps), you can use ACTION_SENDTO with a Uri beginning with the mailto scheme. This will also work if you don't know the recipient beforehand:
final Intent shareIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "The Subject");
shareIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
.append("<p><b>Some Content</b></p>")
.append("<small><p>More content</p></small>")
.toString())
);

Facebook share doesn't work correctly

I am trying to share a text containing a link via facebook.
My code work perfectly with the facebook messenger application. But not via Facebook app.
In Facebook app i am getting a sharing view with an empty edittext. I Don't want to integrate facebook api and give sharing authorisation. I don't want to do that. I think it can be done only via extras and intent.
My sharing code:
private void ShareWebView(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, mTitle);
startActivity(Intent.createChooser(intent, "Share with"));
}
You cannot. See Android and Facebook share intent
And especially this link provided in one of the comment : http://developers.facebook.com/bugs/332619626816423
Walkaround: If you do not want to implement it using android SDK
and you want to use
private void ShareWebView(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, mTitle); // MUST contain 1 url
startActivity(Intent.createChooser(intent, "Share with"));
}
make sure that mTitle contains one LINK.
Why to do that: Although the fact that facebook doesn't work properly it grubs the first url or look like url from the mTitle and post-it as share url. It also automatically catch a subtitle and a photo from that url so the post/share is quite acceptable most of the time avoiding long code implementations!

Android Intent ACTION.SEND -- can't send text to Twitter or Facebook

I am having trouble when I try to pass a text string from within an EditText to either Facebook or Twitter via an intent. In fact, the only option which works currently is email.
The code I am using is below:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, textVariable.getText());
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject Title");
startActivity(Intent.createChooser(intent, "Share Text"));
I have tried it with and without the subject line. Any thoughts?
Thank you :)
To share the text on Facebook and Twitter the way you are using is not correct. You need to try out the other way of sharing.
Just refer Link it will guide you on how to share the text on facebook & twitter using intent.
The following code should do the trick!
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.twitter.android",
"com.twitter.applib.composer.TextFirstComposerActivity"));
intent.putExtra(Intent.EXTRA_TEXT, text);
context.startActivity(intent);
// Success!
} catch (Exception e) {
// official twitter code missing
}
Look Share bitmap with text on Twitter, Email & bitmap on Facebook in Android using Intent
You can share image with text caption on twitter but on facebook you can not share text caption using Intent, for that you have to use Facebook SDK.

Android: action_send intent is not showing facebook

I recently implemented the action-send intent to share a plain text. Facebook is installed and updated on my phone but only "Googlemail" and "Textmessage" are shown as options for sharing my text.
A short code snippet:
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, item.getTitle());
intent.putExtra(
android.content.Intent.EXTRA_TEXT,
(item.getDescription());
startActivity(Intent.createChooser(intent, "Send..."));
Any suggestions what's wrong with my app?
Normally I would think that I dont have to implement the whole facebook sdk for my simple purpose?!
Thanks in advance
The MIME type to use is text/plain, not plain/text.

Android, How to send HTML email and force Android to send it through G-Mail not other applications? [duplicate]

This question already has answers here:
Send HTML mail using Android intent
(5 answers)
Closed 3 months ago.
I want to send email through my application. I need to send HTML based email just through G-Mail. I found following solutions that each of them has pros and cons.
1) Using Intent (Intent.ACTION_SEND). This is very simple way and I can see my body in HTML format but the problem is when I click on "Send email" button, so many applications like Facebook and Google+ pop up which are useless and I shouldn't show it in that list. This is its code:
String html = "<!DOCTYPE html><html><body>Visit W3Schools.com!" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"MY EMAIL ADDRESS"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(html));
startActivity(Intent.createChooser(intent, "Send email..."));
2) Using Intent (Intent.ACTION_SENDTO). This way Filters useless applications and shows me just mail clients. But it doesn't display my email in HTML format in gmail client. When i send the email some clients show the body in HTML format while others doesn't identify HTML and my link behaves like plain text. This code is like:
String html = "<!DOCTYPE html><html><body>Visit W3Schools.com!" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>";
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:MY EMAIL ADDRESS" + "?subject=subject here" + "&body=" + html;
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
3) Sending mail using JavaMail API which adds so much complexity to application and I didn't test it so far.
What is your suggestion? I need a way to have advantages of first and second ways. I need when user click on button it shows Gmail client and I can show him/her html content in body part of client.
any suggestion would be appreciated. Thanks
======================
Update
Something about code 2 is wrong. The code is like this:
String html = "<!DOCTYPE html><html><body>Visit W3Schools.com!" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>";
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:MY EMAIL ADDRESS" + "?subject=subject here" + "&body=" + Html.fromHtml(html);
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
Try the following -
Intent shareIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(body));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
startActivity(shareIntent);
This will only present email applications.
If you want only one application to handle your intent then you need to remove Intent.createChooser(), rather jst use startActivity()---> it send the mail using default email client, if not set then will ask to do so... tat can be changed anytime
To get only email applications, use
Intent.setType("message/rfc822")

Categories

Resources