ACTION_SEND sending both an Image and Text in the same Intent - android

So I would like to do something like:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(myMessageAsImage));
intent.putExtra(Intent.EXTRA_TEXT, "My Message");
intent.setType("text/plain"); // or intent.setType("image/<imageType>");
However the documentation for ACTION_SEND doesn't seem to make this seem possible. Is there an agreed upon way to do this?

I don't know what you mean by 'common way' but I think you should set the type to intent.setType("image/*");.
EDIT:
How you send data with intent depends on the availability of apps that filter your particular action. Apps that handle ACTION_SEND may not handle ACTION_SEND_MULTIPLE. Clicking Share on HTC Gallery produces a list of applications that handle image, single or multiple. If you choose Mail then you can select multiple images. But if you choose Facebook or Peep then you can only select one image. This is my simple solution if you want to do the reverse of HTC Gallery, that is: user chooses image(s) first then you show him all compatible apps based on how many he selected.
// assuming uris is a list of Uri
Intent intent = null;
if (uris.size > 1){
intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
} else if (uris.size() == 1) {
intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));}
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_TEXT, "Some message");
startActivity(Intent.createChooser(intent,"compatible apps:"));

Related

How to combine ACTION_SEND and ACTION_VIEW intent types?

I would like to share a URI from my app, and have the app chooser dialog show options for ACTION_SEND apps (like SMS and copy to clipboard) as well as ACTION_VIEW apps (like Chrome). So far, I can only seem to get one set of apps to show at a time. Is there a way to combine intent actions?
Here is what plain ACTION_SEND intent looks like:
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "www.example.com");
context.startActivity(Intent.createChooser(i, "Share"));
This results in the normal chooser for apps that send information. But no open in browser options.
Here is what ACTION_VIEW intent looks like:
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.putData(Uri.parse("www.example.com"))
context.startActivity(Intent.createChooser(i, "Share"));
This results in the normal chooser for opening the link in the browser. But no options for info sending apps.
Is there a way to "combine" these two behaviors so both sets of options show up in the chooser dialog?
I have also tried to add categories to the intent, but no luck.
EDIT: I stumbled across this question where the OP has the same issue. However, I would like a solution that does not involve creating a bunch of custom activities for each app I'd like to show in the chooser.
I know this is late, but have a different solution:
Create intents for send and view. Create a chooser intent for one of them and pass the other intent as Intent.EXTRA_INITIAL_INTENTS. Like this:
// Share
val sendIntent = Intent(Intent.ACTION_SEND)
sendIntent.setDataAndType(uri, MIME_PDF_TYPE)
sendIntent.putExtra(Intent.EXTRA_TEXT, "TEST")
sendIntent.putExtra(Intent.EXTRA_STREA, uri)
// Open
val openIntent = Intent(Intent.ACTION_VIEW)
openIntent.setDataAndType(uri, MIME_PDF_TYPE)
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val chooserIntent = Intent.createChooser(sendIntent,activity.getString(R.string.sharing_title))
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(openIntent))
activity.startActivity(chooserIntent)
i have kotlin snippet and i'm sharing with you. You can use sharecompact builder.
ShareCompat.IntentBuilder.from(requireActivity())
.setType("text/plain")
.setSubject(getString(R.string.app_name))
.setChooserTitle("Share via")
.setText(your text)
.startChooser()

How to make app picker for sharing text permanent in Android?

I'm trying to make refer and earn activity in my appSo I want to permanently display a few apps like whatsapp, etc for the user to click on them and share directly.I'm using Intent to share the referral code but it pops up the apps list when the user clicks share.The code I'm using is,
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "This is a message");
intent.setType("text/plain");
startActivity(Intent.createChooser(intent, "Share via"));
How can I make the app chooser permanent for a few apps?
The app chooser is not intended to be displayed permanently. Therefore you will have to create simple buttons or icons and create an intent that refers to the desired app directly, by setting the package of the intent.
E.g. to share sth with WhatsApp use sth like this:
Intent sendIntent = new Intent();
// here comes the magic
sendIntent.setPackage("com.whatsapp");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Depending on the type of content you want to share and the apps you want to share with, it makes sense to reuse the code to create the intent and just set the respective package and eventually some additional parameters.
You will need package name of app and a Intent.
change ACTION_VIEW to ACTION_SENDTO
set the Uri as you did set the
package to whatsapp
Intent i = new Intent(Intent.ACTION_SENDTO,
Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
i.setType("text/plain");
i.setPackage("com.whatsapp"); // so that only Whatsapp reacts and not the chooser
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "I'm the body.");
startActivity(i);
You can refer this link for More:
Send text to specific contact (whatsapp)
Sending message through WhatsApp
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT, "your text content");
startActivity(intent)
I am facing same problem for share tamil font content in Whatsapp. I found the solution, this setType("*/*") share full content.

How to set action, category and data for intent?

I'm programming an Android application. I want to invoke other applications to perform certain operations(Sending emails etc.) How do I know which action and category to set for the intent? Should I look other application's intent filter? What if that application is not open source?
Also, for the data or extra attribute, I don't know how the 3rd party application will handle my intent, so I do not know how to set the attributes. For example, I want one string as the title of the email, one string as the content of the email, and another string as the recipient, and a picture as the attachment. Can I include all these information in the intent? What if the 3rd party application don't provide any functionality to handle it?
Usually, for common tasks in Android there is a general Intent that you send on which other applications can register.
for example to share some text you would create an intent like:
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)));
will prompt android's native share dialog on which the user can choose how he wants to share it.
Specifically for email you would do something like:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","email#domain.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "This is my email subject");
startActivity(Intent.createChooser(intent, "Email"));
Other examples may be to launch the default sms application:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", getMessageBody());
Or open the phone's dialer:
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
You need to figure out what are the actions that you want to implement in your app and then figure out how to implement each of them.
You can find some more data here:
Android content sharing
Android intents - under the various intent actions
Try using category and actions in intent.
Intent mailIntent = new Intent(Intent.ACTION_SEND);
mailIntent.setType("text/plain");
mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Reporting mail");
mailIntent.putExtra(Intent.EXTRA_TEXT, "Some message");
mailIntent.putExtra(Intent.EXTRA_EMAIL, "xxx#yyy.com");
startActivity(mailIntent);
this is an example for sending email. For further details refer http://developer.android.com/guide/components/intents-common.html

How to know which application the user chose when using an intent chooser?

I'm using an intent chooser to invite friends :
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, Resources.getString("InvitationSubject", getBaseContext()));
String body = Resources.getString("InvitationBody", getBaseContext()) + Local.User.FirstName;
intent.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(intent, "Invite friends"));
Is there a way to know which app the user selected in the chooser ?
As far as I know, there is no direct way to gather such information. What would you need it for? Imho the android intent system was designed so you, as an app developer, don't have to worry about what app the user chose to handle your intent.

Post different text on facebook and twitter using Android intents

I want to enable the user of my android app to post some data on fb,twitter and email it to someone as well. I am using Intent.ACTION_SEND for this. I can add the email subject and add test as Intent.EXTRA_TEXT. But I want different texts to be sent to dirrerent applications.
Like the text to be sent to twitter will be short, the text to be sent to facebook will have a link and a shot description, and the on ein email have all the content.
How can I achieve such a functionality?
At most I can let facebook and twitter take the same text but different from what it is in email.
First, create an Intent representing what you want to potentially e-mail, post twitter, etc. Put some good default values in the Intent.EXTRA_TEXT and the subject. Then call, Intent.createChooser() with your intent. This method will return an Intent representing which Activity the user selected. Now, here's where we add the customization you want. Examine the Intent that is returned like so:
Intent intentYouWantToSend = new Intent(Intent.ACTION_SEND);
intentYouWantToSend.putExtra(Intent.EXTRA_TEXT, "Good default text");
List<ResolveInfo> viableIntents = getPackageManager().queryIntentActivities(
intentYouWantToSend, PackageManager.MATCH_DEFAULT_ONLY);
//Here you'll have to insert code to have the user select from the list of
//resolve info you just received.
//Once you've determined what intent the user wants, store it in selectedIntent
//This details of this is left as an exercise for the implementer. but should be fairly
//trivial
if(isTwitterIntent(selectedIntent)){
selectedIntent.putExtra(Intent.EXTRA_TEXT, "Different text for twitter");
}
else if(isFacebookIntent(selectedIntent)){
selectedIntent.putExtra(Intent.EXTRA_TEXT, "Different text for facebook");
}
startActivity(selectedIntent);
By examining the Intent that is returned by Intent.createChooser, we can determine how we need to modify it before launching it. You'll have to implement the isTwiterIntent and isFacebookIntent function yourself though. I imagine this will be relatively easy though, as you probably just have to examine the context of the Intent. I'll do a little more research and see if I can't find an exact solution for determining if an Intent is for Twitter or Facebook, or whatever and try to give you a more complete answer.
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
List<ResolveInfo> activities = getPackageManager().queryIntentActivities(sharingIntent, 0);
By this code you get list of applications that support Intent.ACTION_SEND action.
After that u can built a Alert Dialog to display those applications.
then on click listener of the particular application you can make your changes as given code
public void onClick(DialogInterface dialog, int which)
{
ResolveInfo info = (ResolveInfo) adapter.getItem(which);
if(info.activityInfo.packageName.contains("facebook"))
{
shareToFacebook();
}
else {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "hello");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "intent");
startActivity(sharingIntent);
}
}

Categories

Resources