How to send a GIF on Whatsapp programmatically on Android?

Asked

Viewed 136 times

1

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

  • The purpose of @Wallacemaxters' inquiry is to separate the wheat from the chaff. Its original version of the question did not imply that it was related to programming, it seemed more like a call for help to use an app.

  • 3

    I am a well of sweetness, people :)

  • 2

    I really now understand everything you’re saying, when W. Maxters answered I interpreted it differently.

  • 2

    Removed the closing vote,.

1 answer

1


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

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"));
}

I haven’t tested it yet.

  • thanks for replying William, what is this line does Inputstream Fis = getResources(). openRawResource(getResources(). getIdentifier(resourceName, "drawable", getPackageName()));

  • I’ve seen this code more as I’m new at it I don’t understand many new things I see... reading the code with my ignorance I thought about what I said more I didn’t understand because up there have String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
 String fileName = "sharingGif.gif"; and just below try to read the

  • I understand, I will "unlock" the code and explain part by part, it may take a while because I still can’t get a cell phone borrowed to perform the tests.

  • is the internal memory of the phone itself, since a gif is very heavy

  • I prefer it like this, you explaining me, thank you!

  • 1

    Hello Guilherme Nascimento to using this code ta working right, thanks!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.