首页 文章

如何在Android(2016)中分享音频到社交网络(Whatsapp,Facebook)? [重复]

提问于
浏览
-1

这个问题在这里已有答案:

我开发了一个“应用程序”,这是一个来自我国音板的非常受欢迎的人 . 它非常简单,它有6个主要按钮播放不同的声音,然后每个声音有其他2个按钮,一个用于通过社交网络共享声音,另一个用于将声音设置为铃声,警报或通知 . 起初,一切都工作正常,但有一天,突然间,它停止了共享功能(其他功能仍然有效) .

对于我尝试分享的每个社交网络,出现的消息是"The format is incompatible"(或类似的东西,它是西班牙语) . 你可以下载这个应用程序点击此链接download the app here

共享的最后发布代码如下:

private void shareItAYQueNoEntren() {
                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                    sharingIntent.setType("audio/mpeg3");
                    Uri path = Uri.parse("android.resource://fzmobile.elgordodecentral/raw/" + R.raw.yquenoentren);
                    sharingIntent.putExtra(Intent.EXTRA_STREAM, path);
                    startActivity(Intent.createChooser(sharingIntent, "Share by..."));
                }

并且原始文件夹中的音频文件的扩展名为.mp3 .

我该如何解决这个问题呢?

2 回答

  • 0

    试试这个:

    String sharePath = Environment.getExternalStorageDirectory().getPath()
            + "your audio file path here";
    Uri uri = Uri.parse(sharePath);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("audio/*");
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Share Sound File"));
    

    另外,不要忘记添加WRITE_EXTERNAL_STORAGE权限,否则在运行应用程序时会出错 .

  • 0

    将音频文件从资源复制到外部存储,然后共享它:

    InputStream inputStream;
     FileOutputStream fileOutputStream;
     try {
    inputStream = getResources().openRawResource(R.raw.sound);
    fileOutputStream = new FileOutputStream(
            new File(Environment.getExternalStorageDirectory(), "sound.mp3"));
    
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        fileOutputStream.write(buffer, 0, length);
    }
    
    inputStream.close();
    fileOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
     }
    

    然后

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM,
        Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/sound.mp3" ));
    intent.setType("audio/*");
    startActivity(Intent.createChooser(intent, "Share sound"));
    

    为AndroidManifest.xml文件添加WRITE_EXTERNAL_STORAGE权限:

相关问题