分享失败,请重试(仅限whatsapp)

2024-05-11

当我将内容分享到 Whatsapp 时,它返回到分享页面,并显示 Toast 通知“分享失败,请重试”

my code

if (url.startsWith("share://")) {
            Uri requestUrl = Uri.parse(url);
            String pContent = requestUrl.toString().split("share://")[1];
            Toast toast=Toast.makeText(getApplicationContext(),pContent, Toast.LENGTH_LONG);
            toast.setMargin(50,50);
            toast.show();
            StringBuilder sb = new StringBuilder();
            String [] parts = pContent.split("<br />");
            for (int i = 0; i < parts.length; i++) {
                String part = parts[i];
                sb.append(part);
                sb.append('\n');
            }
            Intent share = new Intent();
            share.setAction(Intent.ACTION_SEND);
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            share.putExtra(android.content.Intent.EXTRA_TEXT, (Serializable) sb);
            share.setType("*/*");
            try {
            startActivity(Intent.createChooser(share, "Share On"));
            } catch (android.content.ActivityNotFoundException ex) {
                toast = Toast.makeText(getApplicationContext(), "whatsapp not installed", Toast.LENGTH_LONG);
                toast.setMargin(50,50);
                toast.show();
            }
            return true;

和我的日志猫

08-01 14:37:42.081 1472-1514/com.example.myactivity I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
08-01 14:37:42.081 1472-1514/com.example.myactivity I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
08-01 14:37:42.081 1472-1514/com.example.myactivity I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3

有同样的问题 - 解决方案是定义 MIME 类型:当尝试与文本和附加图像设置共享意图时sharingIntent.setType("*/*")可以正常工作,但是当如上所述仅共享文本时会失败。

解决方案:如果仅共享文本集sharingIntent.setType("text/plain")

public void sendShareToWhatsAppIntent() {

    //setup intent:
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);

    //setup image extra, if exists:
    Bitmap picBitmap = getMyBitmap();
    if (picBitmap != null) {
        String url = MediaStore.Images.Media.insertImage(context.getContentResolver(), picBitmap, "", "");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
        sharingIntent.setType("*/*");
    } else {
    //if no picture, just text set - this MIME
        sharingIntent.setType("text/plain");
    }

    //setup sharing message
    String message = "My Message - hey whatsapp!"

    sharingIntent.putExtra(Intent.EXTRA_TEXT, message.toString());

   //target WhatsApp:
   sharingIntent.setPackage("com.whatsapp");


    if (sharingIntent.resolveActivity(context.getPackageManager()) != null) {
        startActivity(sharingIntent);
    } else {
        Log.w(TAG, "sendShareIntent: cant resolve intent");
        Toast.makeText(context, "whatsapp not installed", Toast.LENGTH_SHORT).show();
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

分享失败,请重试(仅限whatsapp) 的相关文章

随机推荐

  • 将 gsutil 与谷歌驱动器(不是谷歌云存储)一起使用

    gsutil https cloud google com storage docs gsutil csw 1 gettingstarted use 博托配置文件 https cloud google com storage docs gs
  • Web 应用程序的 Spring Boot 和 Comodo https 配置

    我是 https 配置的新手 并尝试配置 spring boot 和 Comodo SSL 经过几次电话和尝试后 我已经弄清楚如何为您的 spring boot 网站正确配置 https 下面的答案是详细步骤 希望它可以帮助人们生成更安全的
  • java.io.FileNotFoundException:/storage/emulated/0/saved_images/grub.jpg:打开失败:ENOENT(没有这样的文件或目录)

    我使用下面的代码将图像保存在 SD 卡中 但我不断收到以下异常 private void SaveImage Bitmap finalBitmap String filename String root Environment getExt
  • Javascript 可折叠面板默认打开

    我正在关注这个代码示例在这里找到 https www w3schools com howto howto js collapsible asp使用 css html javascript 创建可折叠面板 function toggleCol
  • 编辑和导入故事板更改从 Xcode 到 Visual Studio

    我正在将代码从 Xamarin Studio Mac 移动到 Visual Studio 我可以从 Xamarin Studio 在 Xcode 中打开故事板文件 如果我在 Xcode 中进行更改 关闭它 然后在 Xamarin Studi
  • border-box 不适用于内联块元素?

    我有一个清单inline block元素 我想为您将鼠标悬停在其上的元素添加边框 但是 请注意边框如何偏移元素 即使我使用box sizing border box并明确定义元素的宽度和高度 我说明了以下行为 box sizing bord
  • 图片加载性能

    我已经尝试了几个小时从文件加载图像的各种方法 请看一下这两种方法 public Image SlowLoad string path return Image FromFile path public Image FastLoad stri
  • 在同步函数中使用 javascript `crypto.subtle`

    在javascript中 是否可以使用浏览器内置的sha256哈希 https developer mozilla org en US docs Web API SubtleCrypto digest Converting a digest
  • 像盒子一样的液体?

    我正在制作一个响应式网站 需要为客户的 Facebook 粉丝页面添加一个 Facebook Like Box 这点赞框的开发者页面 http developers facebook com docs reference plugins l
  • Android OpenGL ES 2.0 模拟器

    好吧 我有一个支持 OpenGl ES 2 0 HTC Desire 的设备 当然在设备上部署应用程序比在模拟器上部署应用程序要快得多 默认的android模拟器不支持2 0 androidX86项目和类似的androbox项目不支持本机代
  • Python:是否可以向 Tuple 类添加新方法

    在Python中 是否可以向内置类添加新方法 例如Tuple 我想添加 2 个新方法 first 返回元组的第一个元素 second 返回一个没有第一个元素的新元组 例如 x 1 2 3 x first 1 x second 2 3 Tha
  • NSPredicate 格式字符串不起作用

    在我的代码中 我想检查记录是否已存在 以便我知道是创建它还是更新它 但我遇到了一个问题 问题是当我使用这个时 NSPredicate pred NSPredicate predicateWithFormat ATTRIBUTE ID idN
  • 一组记录中某些值相同的唯一约束

    DBMS MS Sql Server 2005 标准版 我想创建一个表约束 以便只有一个记录在表的子集中具有特定值 其中行共享特定列中的值 这可能吗 Example 我的 myTable 中有一些记录 其中有一个非唯一的外键 fk1 以及一
  • 从外部应用程序对流星用户进行身份验证

    我有一个流星应用程序 带有流星帐户 并且正在构建一个移动应用程序 如何通过移动应用程序对用户进行身份验证 我有一个 python web 服务 可以将数据发送到移动客户端 但我不知道如何从外部应用程序进行身份验证 我确实查看了DDP客户端
  • Kubernetes WatchConnectionManager:执行失败:HTTP 403

    我遇到错误Expected HTTP 101 response but was 403 Forbidden 在我使用以下命令设置新的 Kubernetes 集群之后Kubeadm当我提交下面遇到的 pyspark 示例应用程序时 只有一个主
  • 发送网络推送通知时出错:没有端点?

    我创建了一个基本应用程序 尝试使用 web push 节点库发送推送通知 但是 我总是遇到错误 你必须至少传入一个端点 我不明白 因为我 订阅 使用 ServiceWorker Api subscribe 方法 它创建了一个到我的本地主机地
  • 如何将jarray对象添加到JObject中

    如何添加JArray into JObject 我在更改时遇到异常jarrayObj into JObject parameterNames Test1 Test2 Test3 JArray jarrayObj new JArray for
  • 使用python服务帐户管理gmail签名

    我想编辑 Google Apps 域中的用户签名 我计划使用服务帐户 服务帐户被委托给整个域 我使用 gmail API 来发送和检索电子邮件 但使用不同的 api 修改签名 根据https developers google com ad
  • 无法在 Firefox 中显示我的 svg 图像?

    我无法在 Firefox 中显示我的 svn image 它在 Safari 和 Chrome 中工作正常 没有 IE 所以还没有测试过 它在我的html中是这样实现的 img 类 logo1 src images logo6 svg 其中
  • 分享失败,请重试(仅限whatsapp)

    当我将内容分享到 Whatsapp 时 它返回到分享页面 并显示 Toast 通知 分享失败 请重试 my code if url startsWith share Uri requestUrl Uri parse url String p