By searching on the Web I see that there is more than one way to share my App by inviting others to download it.
I tried this code that works, showing up to the user the app chooser pane.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
intent.putExtra(Intent.EXTRA_TITLE, "My subject");
intent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID);
startActivity(Intent.createChooser(intent, "Share App"));
What information must I include in the Intent in order to correctly display the message when the user chooses to share it with WhatsApp, Telegram, SMS, email, etc.?
For example, this code will show a preview in Telegram (with the highlighted link and a preview image) but not in WhatsApp (it show only the plain text to send as a message): why?
I tried also this code but it works for telegram but not for Whatsapp (it send a message with only an attachment unable to open with text “without title”):
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TITLE, "title test");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject test");
String shareMessage= "message testnn";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"nn";
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
shareIntent.putExtra(Intent.EXTRA_HTML_TEXT, "HTML " + shareMessage);
Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ res.getResourcePackageName(R.drawable.testjpg) + '/'
+ res.getResourceTypeName(R.drawable.testjpg) + '/'
+ res.getResourceEntryName(R.drawable.testjpg));
Toast.makeText(this, imageUri.toString(), Toast.LENGTH_LONG).show();
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.share)));
How to make it works for WhatsApp, Telegram, FB, Email and other text-only like SMS?
3
Answers
The following is, for me, the working code using
FileProvider
(tested on a real device with Android6.1
).By using
FileProvider
I can allow other Apps to read my image.AndroidManifest.xml:
provider_paths.xml:
Activity.java:
Note: as @CommonsWare says in his answer:
So this code may not works anymore in the future.
Here is a possible answer –
Hope this helps!!
Source – Sending message through WhatsApp By intent
AND
https://developer.android.com/guide/components/intents-common#java
The definition of “correctly” is up to the developers of those other apps, not you. What they do with the extras on your
Intent
is up to them, not you. What they do with those extras will vary based on the app, the app version, and possibly device/OS characteristics. You have no control over any of that. You simply provide the data and let the developers of the other apps do what they want with it.Because that is what the Telegram and WhatsApp developers chose to do.
There are various bugs in that code:
You are providing something that is not HTML in
EXTRA_HTML_TEXT
image/jpg
is not a valid MIME type (it isimage/jpeg
)Your
Uri
has theandroid.resource
scheme, not thecontent
schemeWhether fixing those bugs will change the behavior of WhatsApp is up to the developers of WhatsApp, and that behavior might change seven times within the next hour, for all you know. So, while I recommend fixing those bugs, do not assume that any given app is necessarily going to behave differently, or in some way that you want.