3

I'm building an app that will ask the user to export a chat from WhatsApp into my app. How can I show my app in the "Send chat via.." intent window?

Javi
  • 889
  • 1
  • 16
  • 41

2 Answers2

6

The correct way to do this is to add the following intent-filter:

    <intent-filter>
        <action android:name="android.intent.action.SENDTO"/>
        <data android:scheme="mailto"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <data android:mimeType="*/*"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <data android:mimeType="*/*"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

Then you can read the content of the chat using this:

            Uri uri = intent.getClipData().getItemAt(i).getUri();
            InputStream inputstream = getContentResolver().openInputStream(uri);
            byte[] data = new byte[1024];
            int bytesRead = inputstream.read(data);

            while (bytesRead != -1) {
                chatContent.append(new String(data));
                bytesRead = inputstream.read(data);
            }

            // TODO - Here we can do whatever we want with the chat content chatContent.toString()
            if (mainTextView != null){
                mainTextView.setText(chatContent.toString());
            }
Javi
  • 889
  • 1
  • 16
  • 41
  • thanks!! can you help me in this question https://stackoverflow.com/questions/55340941/how-to-set-emojis-like-whatsapp-chat-instead-of-mobile-specific – Rucha Bhatt Joshi Mar 25 '19 at 15:16
  • @Javi Where should I add those intent-filters? In my Manifiest? where did you add it usually? – Cristofer Oct 03 '19 at 02:50
  • thanks! With the intent-filter my app is shown to receive the chat export, but I'm unable to manage the intent to receive the chat in my app. My goal would be to save a txt file in the phone's memory, any advice? – ssoBAekiL Dec 27 '19 at 02:30
  • Check you have all the permissions like I did. – Javi Dec 28 '19 at 08:26
  • @Cristofer just add them in the AndroidManifest file – Javi Jun 14 '22 at 20:13
1

To read the content of the chat is this complete:

Intent intent = getIntent();
String type=intent.getType();
    
String action=intent.getAction();
StringBuffer chatContent=new StringBuffer();
// Figure out what to do based on the intent type
if(intent.ACTION_SEND_MULTIPLE.equals(action) && type!=null){
    Bundle bundle=intent.getExtras();
    try {
        for(int i=0;i<intent.getClipData().getItemCount();i++){
            Uri uri = intent.getClipData().getItemAt(i).getUri();
            InputStream inputstream = getContentResolver().openInputStream(uri);
            byte[] data = new byte[1024];
            int bytesRead = inputstream.read(data);

            while (bytesRead != -1) {
                chatContent.append(new String(data));
                bytesRead = inputstream.read(data);
            }
    }
    //aqui se hace lo que se quiera con el chat "chatContent"
    System.out.println(chatContent.toString());
            
    System.out.println(bundle.getString(Intent.EXTRA_TEXT).replaceAll("El historial del chat se adjuntó a este correo como \"Chat de WhatsApp con ","").replaceAll("\".",""));
    }catch (Exception e){
        e.printStackTrace();
    }
}
Alktraz
  • 11
  • 1
  • This post is not answering the question. Though it is useful and related, is yet not an answer. This could be a comment instead, with a link to the snippet if it's too large to fit in the comment. Please, consider removing your answer. – Juan José Melero Gómez Nov 19 '21 at 10:03