要使用 Intent 选择音频文件,我正在使用此功能:-
fun selectAudioFromStorage() {
val pictureActionIntent = Intent(Intent.ACTION_GET_CONTENT, null)
pictureActionIntent.type = "audio/*"
pictureActionIntent.putExtra("return-data", true)
startActivityForResult(pictureActionIntent, ResultConstants.RC_SELECT_AUDIO)
}
片段类的 onActivityResult 提供所选音频文件的 Uri,如下所示:
content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Forganfinale.mp3
在 Android (Lollipop) 上,使用光标将 uri 转换为真实路径可以正常工作,但对于 Oreo 设备,我无法从该音频的 uri 获取真实路径。
由于文件路径在 API 29 及更高版本中不可用于公共文件,我建议您不要尝试使用它们,而是使用 JavaFileDescriptor
反而。
As per https://developer.android.com/training/data-storage#scoped-storage
更具体地说https://developer.android.com/training/data-storage/shared/media#open-file-descriptor
许多 API 允许您提供FileDescriptor
而不是路径。
例如使用https://developer.android.com/reference/android/media/MediaPlayer.html#setDataSource(java.io.FileDescriptor)
代替
https://developer.android.com/reference/android/media/MediaPlayer.html#setDataSource(java.lang.String)
为了得到一个fileDescriptor
从 URI 执行以下操作(在 Java 中)
ParcelFileDescriptor pfd =
this.getContentResolver().
openFileDescriptor(URI, "r");
FileDescriptor fileDescriptor = pfd.getFileDescriptor();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)