我正在开发一个Android应用程序,我需要下载mp3文件
但下载过程应该进入后台。
有没有人对此有任何想法?
答案 0 :(得分:4)
最后我得到了解决方案。
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();
}
对我有用。
答案 1 :(得分:0)
android: Is it possible to dowload file to disk given an xml (from a URL)?
检查一下:带有downloadFileFrom方法的AsyncTask类可以帮助你。