I am new on progamming android and I have some problems on sharing contents.
I did an android app that involves a test. In the last activity I'd like to offer the opportunity to share the users results via shareintent, here a frame of the code:
display= (TextView) findViewById (R.id.tvDisplay);
display.setText("Well Done, your score is" + score ); //score is an int
sharebutton= (Button)findViewById(R.id.sharebutton);
sharebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
results = display.getText().toString();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,"My score in the test is" + results);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "TEST");
startActivity(Intent.createChooser(shareIntent, "Share.."));
}
});
The problem is that when I click on the button, I can share the exact text in all of sharer tools (email, wapp, etc..), but not through Facebook. Why?
I tried to allow to post image and text on the FB wall but I can't give the exact path of the image that is on the drawable Folder.
Thank you for all the advices.
It depends upon Facebook app whether they are accepting text or not via intent,I know they accept images and bitmap files but for text they don't accept it.I've already tried this earlier in one of my own project but couldn't supply text via intent.
The Facebook application does not handle either the EXTRA_SUBJECT or EXTRA_TEXT fields.
I have already answer this question here.May be you can follow this link in case you need reference.
first store image in sdcard then share image on facebook
File filePath = new File(Environment
.getExternalStorageDirectory(),
"/foldername/imagename.jpg");
// share("facebook", filePath.toString());
System.out.println(filePath);
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
List<ResolveInfo> resInfo = getActivity().getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(
android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg"); // put here your
// mime
// type
if (info.activityInfo.packageName.toLowerCase()
.contains("facebook")
|| info.activityInfo.name.toLowerCase()
.contains("facebook")) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT,
"Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT,
"This photo is created by App Name");
targetedShare.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(filePath));
targetedShare
.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0),
"Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}
Related
I created my own file extension (.oli). If the user clicks on a file with this extension my app starts and loads the included data. This works like expected. The problem is I would like to give the user of my app the opportunity to share a file (Example: filename.oli).
I implemented this so far:
public void shareFile(){
File file = getShareableFile(); //Creates a .oli-file
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri uri = Uri.fromFile(file);
shareIntent.setType("*/*"); //Maybe the problem
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, name);
startActivity(Intent.createChooser(shareIntent, getString(R.string.shareDatei)));
}
The problem is, the list of apps that think they could handle sharing my file is very large because of shareIntent.setType("/"); Here are two cases that happen if you share with different apps:
If I choose an Email-app like Gmail to share my file it works how it should. The email includes the file as filename.oli . When I click on it my app gets started.
But If I choose for example the Quickmemo-app I get a message that this file can not be shared by this.
So all in all I just want to show apps in the chooserlist that can handle to share my file with the .oli extension . How do I do that? Thanks in advance!
The setType() intent method actually uses MIME types to filter apps according to the docs. So, you'll only be able to filter the apps available based on the following filters:
1.Text
sendIntent.setType("text/plain");
2. Binary
shareIntent.setType("image/jpeg");
3. Multiple content items
shareIntent.setType("image/*");
EDIT
Here's what I would likely do, I would know which apps I want to be able to share the file, like gmail since you know that works, and I would be selective about which apps are in the list. The following code comes from the answer in this SO link: How to filter specific apps for ACTION_SEND intent (and set a different text for each app)
public void onShareClick(View v) {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
emailIntent.setType("message/rfc822");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));
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));
}
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}
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!"));
}
If the user chooses Twitter, I want to send an abbreviated text field (due to character limit). From this SO post -- Branching the Android Share Intent extras depending on which method they choose to share -- I learned that I can implement targeted intents. However, when I use the code below, a number of apps show as "Android System" and Twitter does not show up in the dialog. I even removed the if block trying to catch Twitter, and it still did not appear.
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
PackageManager pm = rootView.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
for(final ResolveInfo app : activityList){
String packageName = app.activityInfo.packageName;
Intent targetedShareIntent = new Intent(Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
if(TextUtils.equals(packageName, TWITTER_PACKAGE_NAME)){
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data, "Twitter"));
} else {
targetedShareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getTitle());
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data));
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Share this story");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
I'm not really sure what's going on with the following:
Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Share this story");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
In the SO post, targetedShareIntents.remove(0) was used although targetedShareIntents.remove(targetedShareIntents.size() - 1) was proposed as a more reusable solution. Any help or explanations on this is greatly appreciated.
I think TextUtils.equals(packageName, TWITTER_PACKAGE_NAME) can't find exactly com.twitter.android package name, so you need to try below code for match Twitter package name
if(packageName.toLowerCase().startsWith("com.twitter")){
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data, "Twitter"));
} else {
targetedShareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getTitle());
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data));
}
I have a Text in my android activity and I want give the user option to share it on social apps like whatsapp, line, facebook, twitter etc.
But I want to create a custom chooser so that it won't show unintended apps in the chooser.
I'm aware of this snippet
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, "Share via"));
How can I make it so that in the chooser it'd only show the apps which I can specify by their package names.
Thanks
Even though I think this question is duplicated, but since I can't find the duplicated question yet, let me provide an answer first.
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareInent = new Intent(Intent.ACTION_SEND);
shareInent.setType("text/plain");
List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(shareInent, 0);
// put the name of the packages you want in this ArrayList
ArrayList<String> wantedPackage = new ArrayList<>();
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
targetedShare.setType("text/plain");
String infoPackageName = info.activityInfo.packageName.toLowerCase();
if (wantedPackage.contains(infoPackageName)) {
targetedShare.putExtra(Intent.EXTRA_TEXT, "put your text here");
targetedShare.setPackage(info.activityInfo.packageName.toLowerCase());
targetedShareIntents.add(targetedShare);
resPackageNames.add(infoPackageName);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Chooser title");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
Simply want to share a dynamic text string + the URL to the app. The native Android share intent is setup correctly, and works perfect with Twitter, Gmail, etc. But, as many will guess, it does not work with Facebook. Appearantly because Facebook will not accept text in the intent.EXTRA_TEXT field, only a single URL.
Well, my question to y'all is: is there a way to branch off the share intent extras depending on which method they choose to share? for example, if they share via gmail or Twitter, use the existing String + URL (the desired option) EXTRA_TEXT, but if they choose to share via Facebook, only use a URL as the EXTRA_TEXT.
Not really wanting to implement the Facebook Android SDK for such a simple task that is built-in natively in Android.
Appreciate your time and advice.
Tried something like this, but it obviously fails because its only checking if the sharing option exists (when share pops up, not after they click a share method), it doesn't respond when they choose a method.
String shareBody = "app string text " + act_txt + " more text! Get the app at http://www.appurl.com";
PackageManager pm = view.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
for(final ResolveInfo app : activityList) {
Log.i(TAG, "app.actinfo.name: " + app.activityInfo.name);
//if((app.activityInfo.name).contains("facebook")) {
if("com.facebook.katana.ShareLinkActivity".equals(app.activityInfo.name)) {
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://www.appurl.com");
startActivity(Intent.createChooser(sharingIntent, "Share idea"));
break;
} else {
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "app name");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share"));
break;
}
}
found a solution, on this SO question asking for something else: https://stackoverflow.com/a/8550043/1938669
the attempt posted my original question here was close. within that cycle of possible shareIntent List, you need to create a new share intent targeted at the specific sharing choice (like facebook or twitter)
here is a final working solution that shares only a URL if facebook is choosen, otherwise shares the complete text string + url:
public void shareIt(View view){
//sharing implementation
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "string of text " + txt_var + " more text! Get the app at http://someapp.com";
PackageManager pm = view.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
for(final ResolveInfo app : activityList) {
String packageName = app.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "share");
if(TextUtils.equals(packageName, "com.facebook.katana")){
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://someurl.com");
} else {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share Idea");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}