android 从服务器下载文件,在android中从服务器下载mp3文件

如果您想从任何网址播放.mp3文件,请遵循nik建议的代码.

但是,如果要从服务器下载文件并将其存储在SD卡或内部存储设备上的任何位置,请遵循以下代码:

private class DownloadFile extends AsyncTask{

@Override

protected String doInBackground(String... urlParams) {

int count;

try {

URL url = new URL("url of your .mp3 file");

URLConnection conexion = url.openConnection();

conexion.connect();

// this will be useful so that you can show a tipical 0-100% progress bar

int lenghtOfFile = conexion.getContentLength();

// downlod the file

InputStream input = new BufferedInputStream(url.openStream());

OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.mp3");

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {

total += count;

// publishing the progress....

publishProgress((int)(total*100/lenghtOfFile));

output.write(data, 0, count);

}

output.flush();

output.close();

input.close();

} catch (Exception e) {}

return null;

}

编辑:清单权限:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值