【发布时间】:2021-12-11 09:10:20
【问题描述】:
如何从 Android 下载目录中的 URL 下载 .mp3 文件?
使用OutputStream 和InputStream 显然不再起作用了。这是我尝试过的:
public class DownloadFile extends AsyncTask<String, Integer, String> {
private final String videoUrl;
public DownloadFile(String videoUrl) {
this.videoUrl = videoUrl;
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(videoUrl);
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream());
String test = Environment.getExternalStorageDirectory() + "/Download/test.mp3";
OutputStream output = new FileOutputStream(test);
byte[] data = new byte[1024];
output.write(data);
output.flush();
output.close();
input.close();
} catch (Exception ignored) {
}
return null;
}
}
这可以下载文件,但尝试播放时收到错误消息saying that I can't play this type of audio file。
另外,上面的代码来自previous question。
【问题讨论】:
-
请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
-
您没有从输入流中读取数据。应该有一个循环。
-
如果我想播放 mp3 文件而播放器无法识别或无法播放 mp3 文件,我假设我没有下载 mp3 文件 - 或者它是播放器问题,正如我所说。
标签: java android android-studio kotlin