Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to share gif images using intent sharing to available apps?

i want working on sharing .gif with available apps like whatsapp, but unable to get valid Uri of gif present in my drawable resource.

 Uri path = Uri.parse("android.resource://my_package_name/" + R.drawable.gif_1);
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/gif");
    shareIntent.putExtra(Intent.EXTRA_STREAM, path);

there is lot of solution on sharing images but no solution on gif sharing, Please help

like image 824
Electro Avatar asked Sep 21 '16 08:09

Electro


1 Answers

I found my answer, I just created a file with a .gif extension and it worked for me. See the code below:

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"));
}
like image 62
Electro Avatar answered Oct 12 '22 10:10

Electro