What's the proper way to share via Intent.createChooser - android

The problem is I have an article that want to share to other apps, and I want to let the user to choose which app to share to. What I want to share is basically:
the title of the article
the URL of the article
the article content as HTML
the URL with some extra text (such as 'http://foo.com/article share from #FooApp')
All of these fields are optional, but I want to share at least one of them.
Such as when share via SMS or twitter, I want to set the content to part 4. when share via Facebook, I want to set 1, 2, 3 together. And when share via email, I want to set subject as 1 and message as 4.
I know (correct me if I'm wrong) every target intent receiver has it's own logic to pick up the fields it needed. So I want to provide as much information as possible and I wrote the following code:
String message = article.getURL() + " #FooApp";
Intent intent = new Intent().setData(Uri.parse(article.getURL())
.putExtra(Intent.EXTRA_SUBJECT, article.getTitle())
.putExtra(Intent.EXTRA_TEXT, message)
.putExtra(Intent.EXTRA_HTML_TEXT, article.getHTML())
.putExtra("sms_body", message)
...
.setType(HTTP.PLAIN_TEXT_TYPE);
startActivity(Intent.createChooser(intent, "Share to"));
But the problem is, it seems like a trick between setData, putExtra, setType.
For some apps appear in the chooser dialog, when I choose, the confirm share window (of that app) display nothing that I set to the intent. (for some other apps they just say failed to fetch resource)
For the putExtra part, when I add or remove some putExtra code, the target intent receivers diff a lot than I expected.
So the question is: am I doing it the wrong way? Are there some guideline for this problem?

Related

Android: Sort attachments sent by mail

This is a silly and not important subject, but nothing to lose on asking.
My app has a "Share App" feature in main menu which sends a promotion email with text and a set of app screenshots, and in the end it calls the following method:
public static void shareFile(ArrayList<Uri> uris, String fileType)
{
try{
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.setType(fileType);
String strSubject = TMLocale.getStringResourceByName("mainmenu_sharethisapp_subject");
share.putExtra(Intent.EXTRA_SUBJECT, strSubject);
share.putExtra(Intent.EXTRA_TEXT, TMLocale.getStringResourceByName("mainmenu_sharethisapp_recommendtext"));
share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
activity.get().startActivity(Intent.createChooser(share, TMLocale.getStringResourceByName("mainmenu_sharethisapp_shareapptitle")));
}
catch(Exception ex){
Toast.makeText(activity.get(), String.valueOf(ex.getMessage()),Toast.LENGTH_LONG).show();
}
}
This is working great, but what you receive -for example by mail- is an unordered set of attachments, I mean, the attachments order does not correspond to the ArrayList<Uri> order.
I'd like the attachments to be ordered as the ArrayList is, so who receives the email can see how the app works following the attachments (from Gmail -for example- and without downloading them) in a "readable"/"understandable" way, like following the same work sequence a user would follow using the app.
I have made several tests just to confirm attachments always arrive not only unordered, but in a different -maybe random- order.
Is it possible to sort the attachments?
I'd like the attachments to be ordered as the ArrayList is
Then use exactly one attachment, where you attach a document that puts your images in the desired order.
Otherwise... your code is starting an ACTION_SEND_MULTIPLE activity. There are likely thousands of these in the Play Store and elsewhere. How any of them handle your extras is up to their developers, not you or me.

How to share different texts based on package name in Android 10?

My app shares a specific URL to open in other apps, but I want to use a custom URL depending on what app the user is sharing it with. For example, with Gmail I want to use myurl.com?src=gmail, and with FB I want to use myurl.com?src=fb etc.
This normally would have worked with this famous method: https://stackoverflow.com/a/18068122/3015986.
However, in Android 10, that solution no longer works anymore: https://medium.com/#AndroidDeveloperLB/this-wont-work-anymore-on-android-q-1702c19eb7bb
So, what other options are there?
Well there are always options. In this particular case(with the given conditions regarding URL query parameter) I would suggest to create separate sharing method for each platform you want to share your content with rather than use the Android default sharing method.
For example here is a custom sharing for Facebook, etc. The method is quite time consuming though.
But there is a better option to handle such cases. It is a relatively new method to get info about which app was chosen. It is easy. Basically you will need to:
Create a PendingIntent for a BroadcastReceiver and supply its IntentSender in Intent.createChooser() like this:
Intent share = new Intent(ACTION_SEND);
...
PendingIntent pi = PendingIntent.getBroadcast(myContext, requestCode,
new Intent(myContext, MyBroadcastReceiver.class),
FLAG_UPDATE_CURRENT);
share = Intent.createChooser(share, null, pi.getIntentSender());
Receive the callback in MyBroadcastReceiver and look in Intent.EXTRA_CHOSEN_COMPONENT like this:
#Override public void onReceive(Context context, Intent intent) {
...
ComponentName clickedComponent = intent.getParcelableExtra(EXTRA_CHOSEN_COMPONENT);
}
More info here
Though you cannot change your URL for each specific case because the sharing event has already occurred, you can implement separate API call or URL link using which you can gather analytics data, change the outcome of the URL click you sent(if you control the backend, of course) or just show a customized message to the user of your app.
Keep in mind, that for some usecases of this method, URL you share should be unique per user(for ex. to change the outcome of the URL click etc).
Hope it helps.

Is there any alternative for Android Share intent not working after 5.0 poperly?

I am creating message having text and url. But when I call share intent it only shows Extra text (that is for link) , doesn't show extra subject( that is for text), is there any solution for that?

How to share a link which is not visible to user

In my android app i want to share a Link to my website using intent but i dont want it to be visible to other user
example i want to share "smoe website link"
But to user it should look like "Click me to see it".
I tried this but wasnt successfull it just shows the simple text and was not clickable
<string name="app_link">Click me!</string>
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra("PostID", postID);
intent.putExtra("UserID", userID);
intent.putExtra(android.content.Intent.EXTRA_TEXT,activity.getString(R.string.app_link);
activity.startActivity(Intent.createChooser(intent, "Share"));
Any help will be really appreciated.
What you want is not realistically possible right now.
EXTRA_TEXT must always be interpreted as plain text by the receiving app. You could try using EXTRA_HTML_TEXT which was added with API 16. But many apps don't support HTML and will simply use EXTRA_TEXT instead (or not show any text at all if you omit EXTRA_TEXT).

how to select attachment in android: similar to email

I have an app. It's not an email app. But it has a feature similar to email, where a user may select to send an attachment along with a message. When I click to add attachment on my gmail, for example, I get the option to attach anything such as video, music, picture, file, etc. Has anyone ever build such an intent and unmarshalled the data and don't mind sharing their expertise?
edit
I am not able to get the multiple mime types in. I need the intent to be able to get video or image or music:
public void onGetAttachmentClicked(View view) {
Intent attachment = new Intent(Intent.ACTION_PICK);
attachment.setType("image/*,video/*,audio/*");
startActivityForResult(attachment, ATTACHMENT_REQUEST_CODE);
}
Right now it only launches the Photos or Gallery app. It does not load for audio, whereas the Gmail app attachment button does.
I think I got it, but I can't say 100% yet so I will reward #CommonsWare after testing
public void onGetAttachmentClicked(View view) {
Intent attachment = new Intent(Intent.ACTION_GET_CONTENT);
attachment.setType("*/*");
startActivityForResult(Intent.createChooser(attachment, "Pick Attachment"),
ATTACHMENT_REQUEST_CODE);
}
If you are trying to get content based on MIME type, use an ACTION_GET_CONTENT Intent with startActivityForResult(). The Uri delivered to your onActivityResult() method will point to the piece of content that the user chose.
I think you should simulate a html form(by assembling http header, parameters), and post your attachment to your server likes picking a file then submit the form

Categories

Resources