Android与WhatsApp共享gif

时间:2018-04-13 05:39:38

标签: android whatsapp

我有以下代码从服务器下载图像并将该图像共享到应用程序。我测试过的所有其他应用(环聊,Android消息,Facebook Messenger)共享都能正常运行,但当我尝试将相同的图像分享给WhatsApp时,我得到了#34;文件格式不支持。"有没有正确的方法与WhatsApp共享一个GIF

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

1 个答案:

答案 0 :(得分:0)

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

}

值得注意的部分:

shareIntent.setType("image/gif");