繁体   English   中英

如何将音频文件从我的应用程序发送到 Whatsapp?

[英]How can I send audio files from my app to Whatsapp?

我正在开发一个应用程序,您可以从中将音频发送到 whatsapp。 但是,我使用的代码不起作用。

//This code returns an error and asks to try again to send the audio file
String sharePath = Environment.getExternalStorageDirectory().getPath()
                                + "android.resource://"
                                + wspActivity.this.getPackageName() + "/raw/" + "increible";
                        Uri uri = Uri.parse(sharePath);
                        Intent share = new Intent(Intent.ACTION_SEND);
                        share.setPackage("com.whatsapp");
                        share.setType("audio/*");
                        share.putExtra(Intent.EXTRA_STREAM, uri);
                        startActivity(Intent.createChooser(share, "Share Sound File"));


//This code indicates that the file is of an incompatible type.
final Intent share = new Intent(Intent.ACTION_SEND);
                        share.setType("audio/mp3");
                        final String path = copyFiletoExternalStorage(R.raw.increible, "increible.mp3");
                        final Uri soundUri = Uri.parse(path);
                        share.putExtra(Intent.EXTRA_STREAM, soundUri);
                        startActivity(Intent.createChooser(share, "Compartiendo..."));
If anyone has any idea why this behaviour, I would appreciate it if you could share it :)

我需要改进其中一些代码,以便能够通过 whatsapp 从我的应用程序发送音频文件

https://faq.whatsapp.com/511210730860677/?cms_platform=android

Android 意图系统 与 Android 上的大多数社交应用程序一样,WhatsApp 听取意图以共享媒体和文本。 例如,只需创建一个共享文本的意图,系统选择器就会显示 WhatsApp: Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "这是我要发送的文本。"); sendIntent.setType("文本/纯文本"); 启动活动(发送意图); 但是,如果您更喜欢直接分享到 WhatsApp 并绕过系统选择器,您可以通过在您的意图中使用 setPackage 来实现:sendIntent.setPackage("com.whatsapp"); 这将在您调用 startActivity(sendIntent) 之前简单地设置; 同样,您可以使用 Android 的意图系统通过 WhatsApp 发送媒体,如果您只想发送到 WhatsApp,则再次使用 setPackage 将 package 限制为 WhatsApp。 查看此开发者页面以获取更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM