android - 如何制作一个共享我的 res/raw/xxx.mp3 文件的按钮

标签 android mp3 share whatsapp

我的应用程序使用 MediaPlayer 的 MP3 文件,我想制作一个按钮,然后将 MP3 文件共享到 whatsapp。

我的代码是这样的:

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    String audioClipFileName="bell.mp3";
    sendIntent.setType("audio/mp3");
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+"/sdcard/"+audioClipFileName));
    startActivity(sendIntent);

但它不工作。为什么不工作?我该如何解决这个问题?

最佳答案

您需要使用环境而不是硬编码路径,例如,如果您的文件位于 sdcard 根目录中,请使用如下代码:

File root = Environment.getExternalStorageDirectory().getPath();
String fname = "bell.mp3";
file = new File(root, fname);

Intent shareCaptionIntent = new Intent(Intent.ACTION_SEND);
shareCaptionIntent.setType("audio/mp3");
shareCaptionIntent.putExtra(Intent.EXTRA_TEXT, "YOURTEXT");
shareCaptionIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.toString()));
startActivity(Intent.createChooser(shareCaptionIntent, "Share in:"));

如果一个资源使用这个:

Uri.parse("android.resource://com.my.package/raw/" + fname);

或资源ID

Uri.parse("android.resource://com.my.package/" + R.raw.bell.mp3);

任何错误请分享 logcat

关于android - 如何制作一个共享我的 res/raw/xxx.mp3 文件的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30915158/

相关文章:

c# - 将 “Everyone” 权限添加到文件夹的网络共享

java - Android 中的线程未按预期工作

android - BitmapFactory.decodeFile();

带有 mp3 header 的 php 将无法加载(更新/修复)

python - pydub-内存错误

ffmpeg - 无法使用 ffmpeg 将 .mp3 转换为 .m4a

android - Facebook 通过 android 应用程序共享 - 更改 "shared via <AppName>"的 url

java - 如何在两个 Java Web 服务之间共享内存?

android - 对于 Windows,Gradle 文件路径太长

android - 无法将 .aar 与依赖项一起发布?