【问题标题】:Android share gif to WhatsAppAndroid 分享 gif 到 WhatsApp
【发布时间】:2018-04-13 05:39:38
【问题描述】:

我有以下代码从服务器下载图像并将该图像共享到应用程序。我测试过的所有其他应用程序(环聊、Android 消息、Facebook Messenger)共享工作正常,但是当我尝试将相同的图像共享到 WhatsApp 时,我得到“不支持文件格式”。是否有正确的方法将 gif 分享到 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"));
    }
} 

【问题讨论】:

  • 你确定whatsapp支持gif吗?如果没有,那就没有办法了。
  • intent.setType("image/gif") 指定哪些应用程序接受 gif 文件进行共享,所以我假设是这样,因为它是一个显示为共享选项的应用程序
  • 也许吧。您仍然有可能该应用程序支持您的图像不包含的 gif 子集,或者它不只是说它支持 image/* 是惰性的并且它仍然不支持 GIF。两个我都见过。我只是不知道哪些应用是那些坏演员之一
  • 你能解决吗?我也有同样的问题!在实体手机中,android 8 在模拟器中运行,android 8 给出了同样的错误。

标签: android whatsapp


【解决方案1】:

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

【讨论】:

  • 此解决方案不适用于 whatsapp,但适用于我上面指定的所有其他应用程序(即环聊、Android 消息、Facebook Messenger)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 2017-12-07
  • 2022-01-14
相关资源
最近更新 更多