2017-04-15 105 views
0

看来我得到的错误。至少我认为它是一个错误。我编写了一种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或Bluetooth进行相同操作时,似乎出现了一些错误。请注意,音频的文件类型是WAV。我也确保我有权写入外部存储,但仍然只有一些应用程序可以工作。我真的失去了这一点,并会很感激,如果有人可以帮助我。

+0

“需要注意的是音频文件类型为WAV” - 然后使用MIME类型WAV。使用'MimeTypeMap'来查找它,因为似乎有一些可能的MIME类型。就目前而言,接收者不知道文件格式是什么。 – CommonsWare

+0

我不明白它将如何帮助例如Google Drive知道它正在上传的文件类型。为什么它甚至需要知道,它只是上传它 – Skrelp

回答

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")); 
} 
相关问题