从服务器的Andr​​oid下载音频 [英] Android download Audio from server

查看:146
本文介绍了从服务器的Andr​​oid下载音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过网址从我的服务器到dowanload音频当我点击该按钮。

I want to dowanload Audio via url from my server when I click the button.

我怎么办呢?

推荐答案

有关DownloadFile从服务器可以使用以下code。

For DownloadFile from server you can use following code.

private class DownloadFile extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... url) {
    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;
}

清单权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

另请参考以下链接它可能会帮助您:

Also refer following links it may helps you :

<一个href=\"http://stackoverflow.com/questions/7351940/how-can-i-download-audio-file-from-server-by-url\">how我可以通过网址从服务器下载音频文件

how can i download audio file from server by url

<一个href=\"http://stackoverflow.com/questions/16117067/androidhow-to-download-the-file-from-the-server-and-save-it-in-specific-folder\">Android:How从服务器下载文件并将其保存在特定的文件夹在SD卡

这篇关于从服务器的Andr​​oid下载音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆