skip to Main Content

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


  1. Chosen as BEST ANSWER

    The following is, for me, the working code using FileProvider (tested on a real device with Android 6.1).

    By using FileProvider I can allow other Apps to read my image.

    AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="app.example.com">
        <application
            ...>
            <activity
                ...></activity>
            <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="app.example.com"
                android:exported="false"
                android:grantUriPermissions="true"
                android:readPermission="app.example.com.fileprovider.READ">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/provider_paths"/>
            </provider>
        </application>
    </manifest>
    

    provider_paths.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-files-path name="files" path="/" />
    </paths>
    

    Activity.java:

    String nomeApp = getString(R.string.app_name);
    String titoloApp = getString(R.string.titolo_app);
    String shareMessage = "Text messagenn";
    shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"nn";
    
    // use the dedicated external directory so the App doesn't need to ask for permission in manifest
    File dirSaveFile = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    
    // create needed dirs for file path
    File imagePath = new File(dirSaveFile, "external_files");
    imagePath.mkdir();
    
    // create empty file
    File imageFile = new File(imagePath.getPath(), "test.jpg");
    
    // get the Bitmap of the drawable to show
    final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.testjpg);
    
    // write in the file the drawable image
    try {
        FileOutputStream fos = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    // create the uri
    Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, imageFile);
    
    // create the Intent
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_TITLE, "Title test");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject test");
    shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("text/plain");
    
    startActivity(Intent.createChooser(shareIntent, getString(R.string.share)));
    

    Note: as @CommonsWare says in his answer:

    What they do with the extras on your Intent is up to them, not you.

    So this code may not works anymore in the future.


  2. Here is a possible answer –

    Intent waIntent = new Intent(Intent.ACTION_SEND);
    waIntent.setType("text/plain");
    String text = "Sorry For Interruption,I'm Just Trying Something";
    waIntent.setPackage("com.whatsapp");
    
    if (waIntent != null) {
        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        waIntent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
        waIntent.putExtra(Intent.EXTRA_TITLE, "My subject");
        waIntent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID);
        waIntent.putExtra(Intent.EXTRA_STREAM, attachment);
        startActivity(Intent.createChooser(waIntent,"Share with"));
    

    Hope this helps!!

    Source – Sending message through WhatsApp By intent

    AND

    https://developer.android.com/guide/components/intents-common#java

    Login or Signup to reply.
  3. 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.?

    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.

    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?

    Because that is what the Telegram and WhatsApp developers chose to do.

    I tried also this code but it works for telegram but not for whatsapp

    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 is image/jpeg)

    • Your Uri has the android.resource scheme, not the content scheme

    Whether 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search