【发布时间】:2011-04-14 09:54:58
【问题描述】:
我正在开发一个 android 应用程序,我需要下载 mp3 文件
但下载过程应该在后台进行。
有人对此有任何想法吗?
【问题讨论】:
我正在开发一个 android 应用程序,我需要下载 mp3 文件
但下载过程应该在后台进行。
有人对此有任何想法吗?
【问题讨论】:
我终于找到了解决办法。
public void mp3load() {
URL url = new URL(url);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v(LOG_TAG, "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
String fileName = "test.mp3";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
}
它对我很有用。
【讨论】:
android: Is it possible to dowload file to disk given an xml (from a URL)?
检查一下:带有 downloadFileFrom 方法的 AsyncTask 类将帮助您。
【讨论】: