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