共享音频文件仅适用于某些应用

时间:2017-04-15 20:34:32

标签: java android libgdx

似乎我遇到了错误。至少我认为这是一个错误。我编写了一种Soundboard,并希望增加与他人分享声音的能力。我要分享的代码如下:

public void share(Sound sound) {
    //libgdx command to copy an internal file(classpath) to external memory
    //sound.file is the relative path to the audio file that will be shared
    Gdx.files.internal(sound.file).copyTo(Gdx.files.external("/skrelpoid/KJ/share/" + sound.file));
    String sharePath = Environment.getExternalStorageDirectory().getPath() + "/skrelpoid/KJ/share/" + sound.file;
    Uri uri = Uri.parse(sharePath);
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
    sendIntent.setType("audio/*");
    //app is the main application
    app.startActivity(Intent.createChooser(sendIntent, "Teile den Sound"));
}

我遇到的问题是这有效,但只适用于某些应用。例如,当我与WhatsApp或DropBox共享音频时,它似乎工作得很好,但是当我尝试使用Google Drive或蓝牙时,似乎有一些错误。请注意,音频的文件类型是WAV。我还确保我有权写入外部存储,但仍然只有一些应用程序工作。我真的迷失了,如果有人能帮助我,我会很感激。

1 个答案:

答案 0 :(得分:0)

似乎错误是因为Uri.parse(str)。 以下代码(取自here)似乎有效:

public void share(Sound sound) {
    Gdx.files.internal(sound.file).copyTo(Gdx.files.external("/skrelpoid/KJ/share/" + sound.file));
    File file = new File(Environment.getExternalStorageDirectory().getPath() + "/skrelpoid/KJ/share/" + sound.file);
    Uri uri = Uri.fromFile(file);
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
    sendIntent.setType(MimeTypeMap.getSingleton().getMimeTypeFromExtension(sound.getExtension()));
    app.startActivity(Intent.createChooser(sendIntent, "Teile den Sound"));
}