0

I have the following code to download an image from the server and share that image to an app. All other apps I've tested (Hangouts, Android Messages, Facebook Messenger) the sharing works correctly but when I try to share the same image to WhatsApp I get "The file format is not support." Is there a correct way to share a gif to WhatsApp

package com.company.package;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v4.content.FileProvider;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;

import java.io.File;

class ShareTask extends AsyncTask<String, Void, File> {
    private final Context context;
    private static final String AUTHORITY = "com.company.package.fileprovider";

    public ShareTask(Context context) {
        this.context = context;
    }
    @Override
    protected File doInBackground(String... params) {
        if (params.length == 0) return null;
        String url = params[0];
        try {
            return Glide
                    .with(context)
                    .load(url)
                    .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .get() // needs to be called on background thread
                    ;
        } catch (Exception ex) {
            Log.w("SHARE", "Sharing " + url + " failed", ex);
            return null;
        }
    }
    @Override
    protected void onPostExecute(File result) {
        if (result == null) { return; }
        File f = new File(result.getAbsolutePath() + "share.gif");
        Log.d("ShareTask", f.getPath());
        result.renameTo(f);
        Uri uri = FileProvider.getUriForFile(context, AUTHORITY, f);
        Log.d("ShareTask", uri.toString());
        share(uri); // startActivity probably needs UI thread
    }

    private void share(Uri uri) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/gif");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        context.startActivity(Intent.createChooser(intent, "Share image"));
    }
} 
  • Are you sure that whatsapp supports gif? If it doesn't, then there's no way to do it. – Gabe Sechan Apr 13 '18 at 05:48
  • intent.setType("image/gif") specifies which apps accept gifs for sharing so I am assuming so since it is an app that shows up as a share option – Android Amdin Apr 13 '18 at 06:00
  • Maybe. You still have the possibility that the app supports a subset of gif that your image isn't in, or that it didn't just say it supported image/* to be lazy and it still doesn't support GIF. I've seen both. I just don't know of what's app is one of those bad actors – Gabe Sechan Apr 13 '18 at 06:02
  • Can you solve it? I have the same problem! In the physical phone android 8 works in the emulator android 8 gives the same error. – Will Sep 24 '18 at 20:27

1 Answers1

0

Taking answer from here

private void ShareGif() {

// replace the path and file name with your gif image path and name
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "temporary_file.gif";

File sharingGifFile = new File(baseDir, fileName);

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

}

Notable part:

shareIntent.setType("image/gif");
nimi0112
  • 2,065
  • 1
  • 18
  • 32
  • This solution doesn't work with whatsapp but does work with all the other apps I specified above (namely Hangouts, Android Messages, Facebook Messenger) – Android Amdin Apr 14 '18 at 20:38