【发布时间】:2018-10-31 05:01:24
【问题描述】:
我正在尝试将音频文件转换为字节数组。音频文件是通过附加文件获取的,因此它在 Uri 中
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
//FileInputStream = reading the file
fis = new FileInputStream(new File(audioFxUri.getPath()));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
byte[] bbytes = baos.toByteArray();
我遵循了this one 的代码,但是一旦我跑了。它给了我一个错误
W/System.err: java.io.FileNotFoundException: /external/audio/media/646818: open failed: ENOENT (No such file or directory)
我也尝试使用 MediaPlayer 播放它并将 Uri 作为其源
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(AttachAudio.this,audioFxUri.toString());
player.prepare();
player.start();
确认音频文件是否正常工作。附言。成功了。
我做错了什么,为什么它没有转换为字节数组?
【问题讨论】:
-
检查外部读取权限
-
@VineshChauhan 是的,我在清单上有权限。