以编程方式从URL下载Android中的Mp3

时间:2013-04-09 18:05:31

标签: java android

我想从网络服务器下载带有声音网址的Mp3声音。我不知道最好的办法是什么!我也看过这些问题。请指引我正确的方向。

Downaload a file programatically on android

What is best way to download files from net programatically in android?

1 个答案:

答案 0 :(得分:0)

试试此代码并告诉我

File output = new File(Environment.getExternalStorageDirectory(),
    fileName);
if (output.exists()) {
  output.delete();
}

InputStream stream = null;
FileOutputStream fos = null;
try {

  URL url = new URL(urlPath);
  stream = url.openConnection().getInputStream();
  InputStreamReader reader = new InputStreamReader(stream);
  fos = new FileOutputStream(output.getPath());
  int next = -1;
  while ((next = reader.read()) != -1) {
    fos.write(next);
  }