[英]How can I download a mp3 file on Android?
How can I download a .mp3 file from a URL in the Android download directory?如何从 Android 下载目录中的 URL 下载 .mp3 文件?
Using OutputStream and InputStream apparently doesn't work anymore.使用OutputStream和InputStream显然不再起作用。 Here's what I've tried:这是我尝试过的:
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;
}
}
This can download the file, but when trying to reproduce it I receive a error message saying that I can't reproduce this type of audio file .这可以下载文件,但是在尝试复制它时,我收到一条错误消息,说我无法复制这种类型的音频文件。
Also, the code above was get from a previous question .此外,上面的代码来自上一个问题。
Did you know the DownloadManager Class From Android ( https://developer.android.com/reference/android/app/DownloadManager )您是否知道 Android 的 DownloadManager 类( https://developer.android.com/reference/android/app/DownloadManager )
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("your url here"));
request.setDescription("Downloading");
request.setMimeType("audio/MP3");
request.setTitle("File :");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "audio.mp3");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.