Android Share Intent chooser - Sharing TEXT with Facebook/Twitter Social media etc - java

If I only send text, the Share Intent chooser DOES NOT give Facebook/Twitter as an option.
Only Gmail, Skype and Evernote are options.
Here is my code
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("plain/text");
shareIntent.putExtra(Intent.EXTRA_TEXT, text)
startActivity(Intent.createChooser(shareIntent, "Share using"));
I have tried different combination of setType() with no joy including "text/*", "text/html" and passing HTML text in the putExtra as follows:
shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text that will be shared.</p>"));
When I use "text/plain", Facebook comes up as an option but the text does not load up when selecting it. But the text does load for Twitter,email, SMS.
Has anyone else encountered this problem?
When I share an image, there is no problem and Facebook along with other social media apps are available on the list.

Also it should be "text/plain" and not "plain/text" according to the documentation.

this depends upon what intent filters are defined by each of those apps.
For instance if I add intent-filter
android.intent.action.send
If I choose single image from Gallery my application will appear in the list. However if I choose multiple my application will not appear as I have not added the intent-filer for android.intent.action.send_multiple
So it depends upon what intents facebook is filtering for. You need to see the release notes or help or developer pages for that.

The facebook issue is a limitation in facebook permissions. Use the facebook API

Share via Twitter :
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)v.getTag(R.drawable.ic_launcher));
// for finding twitter package name ---- >>
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Facebook
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)
v.getTag(R.drawable.ic_launcher));
// finding facebook package name
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("facebook"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Gmail
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT(String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT(String)v.getTag(R.drawable.ic_launcher));
// finding gmail package name ---
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("gmail"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}

Related

Image sharing to the other application by Intent

I am making an app and I wanna share images from my app to a specific app
so i used this code to share via choose
try {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
// path of image
final String imageURIPath = "image-path";
Uri imageURI = Uri.parse(imageURIPath);
sharingIntent.setType("image/jpeg");
sharingIntent.setType("image/jpg");
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageURI);
startActivity(Intent.createChooser(sharingIntent, "Share via app"));
} catch (Exception e) {
showMessage(e.toString());
}
and all work perfectly but I need to share to a specific app like this
shareIntent = getPackageManager().getLaunchIntentForPackage("app_PackageName");
startActivity(shareIntent);
so 'app_PackageName' chage to like app what i need like this example
shareIntent = getPackageManager().getLaunchIntentForPackage("com.adobe.lrmobile");
startActivity(shareIntent);
and I build the app without errors but when I wanna share to that app it just opens that app

How to open my Image in one specif application from the list of different application?

I want to open the image in the specific application by default from the list of different gallery application. I am able to get the list of available application.
But I want to choose one specific Application by default without displaying the list of application. it is possible in Phone setting but I want to do it programmatically.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
PackageManager pm = getPackageManager();
List<ResolveInfo> resInfo = pm.queryIntentActivities(intent, 0);
List<String> intentList = new ArrayList<>();
String packageName = null;
for (int i=0;i<resInfo.size();i++){
ResolveInfo ri = resInfo.get(i);
packageName = ri.activityInfo.packageName;
intentList.add(packageName);
}
startActivity(intent);
Thanks for the help in Advance.....
You need to pass in the app's package name in setPackage() when creating your intent.
intent.setPackage("com.example.gallery");
startActivity(Intent.createChooser(intent, "View Image"));

Open story page of instagram to share image or video

I want to share my image into instagram story. Now I can open istagram easily like this , but this code show me the page that we share photo.
I dont want this one, I want to open instagram story page just.
Here with this code:
try {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
File media = new File(path);
Uri screenshotUri = Uri.fromFile(media);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
sharingIntent.setPackage("com.instagram.android");
startActivity(sharingIntent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(SavedActivity.this, "برنامه اینستاگرام یافت نشد.", Toast.LENGTH_LONG).show();
}
Is it possible to share directly into story of instagram?
This is not possible (not to mention bad practice).
You can only dictate which application(s) can handle your intent, not what they do with that intent once it is received.
It is also not good practice to hardcode the application package you want to send to within your codebase (i.e. "com.instagram.android"). The Android documentation as well as the Instagram developers documentation recommends that you use the standard "chooser" pattern to share photos. In which case, the user will decide which app to share the photo with.
Intent share = new Intent(Intent.ACTION_SEND);
share.setType(type);
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
share.putExtra(Intent.EXTRA_STREAM, uri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(Intent.createChooser(share, "Share to"));
} else {
Toast.makeText(this, "No application to share with", Toast.LENGTH_SHORT).show();
}
Unless this is a personal project (it will not make it to the Google Play Store), I suggest you stick to the above pattern.
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(String.valueOf(dest));
sharingIntent.setType("video/*");
sharingIntent.setPackage("com.instagram.android");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share Video "));
I hope it helps someone .

Android how to get name of app used to share

I'm using the following method to share an image with any app the user picks.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
File imageFileToShare = new File(imagePath);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFileToShare));
if (!TextUtils.isEmpty(text)) {
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
}
context.startActivity(Intent.createChooser(shareIntent, "Share with").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
This works fine, but now I'm trying to get the name of the app picked by the user for sharing. Is there a (standard) way to do this?
Thanks for the attention.
Jose
This is not supported prior to Android 6.0, when using the Android chooser. On Android 6.0+, you can use EXTRA_CHOOSER_REFINEMENT_INTENT_SENDER to provide an IntentSender that will be notified when the user makes a choice and what that choice is.
You are welcome to use PackageManager and queryIntentActivities() to find out what activities support your shareIntent and create your own UI for the user to choose from. Then, since it is your own UI, you will find out what the user chose.
You can try with this code:
PackageManager packageManager = getPackageManager();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
getNameFromApp(packageManager);
And
public String getNameFromApp(PackageManager packageManager) {
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(shareIntent, 0);
int lastDot = 0;
String name = "";
for (int i = 0; i < resolveInfos.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo resolveInfo = resolveInfos.get(i);
String packageName = resolveInfo.activityInfo.packageName;
lastDot= packageName.lastIndexOf(".");
name = packageName.substring(lastDot + 1);
}
return name;
}
This code gets last app's name. But You can do it however you want to. Just do it!.

Restricting share options

Is there a way to restrict share options in an Android app? I have tried using the ShareActionProvider or just simply starting an intent using the Intent.ACTION_SEND intent option. Basically I want to be able to restrict sharing through email only or something of the sort.
you can use something like this, but instead of facebook look for a different name
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"this is a string");
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri); //Share the image on Facebook
PackageManager pm = getApplicationContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
startActivity(shareIntent);
break;
}
}
You can customize the intent chooser as per your need like this -
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject to be shared");
if (StringUtils.equals(packageName, "com.facebook.katana")){
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://link-to-be-shared.com");
}else{
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text message to shared");
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
Hope this helps you.

Categories