【问题标题】:share audio file with whatsapp (file format not supported error)与 whatsapp 共享音频文件(文件格式不支持错误)
【发布时间】:2020-06-16 09:59:42
【问题描述】:

我正在尝试制作一个用于共享音频文件的按钮。我收到文件格式不受支持的错误。我尝试了所有类型但总是给出相同的错误(音频/mpeg、音频/aac、音频/wav、音频/ogg,音频/midi,音频/x-ms-wma) 这是我的代码

 button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        File f = new File("ses.mp3");
                        Uri uri = Uri.parse("file://" + f.getAbsolutePath());
                        Intent share = new Intent(Intent.ACTION_SEND);
                        share.putExtra(Intent.EXTRA_STREAM, uri);
                        share.setType("audio/mp3");
                        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        mactivity.startActivity(Intent.createChooser(share, "Share audio File"));

                        Toast.makeText(getApplicationContext(), "Song Shared Successfully", Toast.LENGTH_SHORT).show();
                    }
                });

【问题讨论】:

  • 我的权限 =
  • 首先,如果您在 Android 7.0 或更高版本上运行此程序,您应该会因FileUriExposedException 而崩溃。其次,new File("ses.mp3") 将导致所有版本的 Android 上的 File 对象无效。该文件在设备上的什么位置?也许我们可以为您指出构造File 对象的更好方法。
  • 我的语音资源路径 res/raw/ses.mp3
  • 如果您是 Android 新手,也许您最好从书本或课程中学习 Android 应用程序开发。您可以从this page 下载我的一本书的免费旧版本。 Version 7.2 是目前最新的,它包含一个符合您需要的示例。但是,它使用assets/ 代替res/raw/ 并使用ACTION_VIEW 代替ACTION_SEND。请参阅从第 2088 页开始的 FileProvider 部分。
  • 请分享整个错误信息。

标签: java android file audio voice


【解决方案1】:

我不确定,但请尝试使用此 Intent

意图分享 = 新意图(Intent.ACTION_SEND);

   share.setType("*/*");

   File imageFileToShare = new File(f.getAbsolutePath());

    Uri uri= FileProvider.getUriForFile
            (mactivity,mactivity.getPackageName()+".provider",imageFileToShare);

    share.putExtra(Intent.EXTRA_STREAM, uri);

mactivity.startActivity(Intent.createChooser(share, "共享音频文件"));

【讨论】:

    猜你喜欢
    • 2016-12-09
    • 2016-10-16
    • 2018-01-26
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    相关资源
    最近更新 更多