How to send a GIF on Whatsapp programmatically on Android?

How can I send a GIF located in the internal memory of my app directly to Whatsapp programmatically?

Author: Guilherme Nascimento, 2017-11-29

1 answers

As per this answer https://stackoverflow.com/a/39656904/1518921 can do this (but of course it will depend on the Whatsapp version)

private void shareGif(String resourceName){

    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    String fileName = "sharingGif.gif";

    File sharingGifFile = new File(baseDir, fileName);

    try {
        byte[] readData = new byte[1024*500];
        InputStream fis = getResources().openRawResource(getResources().getIdentifier(resourceName, "drawable", getPackageName()));

        FileOutputStream fos = new FileOutputStream(sharingGifFile);
        int i = fis.read(readData);

        while (i != -1) {
            fos.write(readData, 0, i);
            i = fis.read(readData);
        }

        fos.close();
    } catch (IOException io) {
    }

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/gif");
    Uri uri = Uri.fromFile(sharingGifFile);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(shareIntent, "Share Emoji"));
}

Haven't tested yet.

 1
Author: Guilherme Nascimento, 2017-11-29 15:04:10