如何通过 url 从服务器下载音频文件

2024-04-01

如何通过 url 从服务器下载音频文件并将其保存到 SD 卡。

我正在使用下面的代码:

public void uploadPithyFromServer(String imageURL, String fileName) {

    try {
        URL url = new URL(GlobalConfig.AppUrl + imageURL);
        File file = new File(fileName);

        Log.d("ImageManager", "download begining");
        Log.d("ImageManager", "download url:" + url);
        Log.d("ImageManager", "downloaded file name:" + fileName);
        /* Open a connection to that URL. */
        URLConnection con = url.openConnection();

        InputStream is = con.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is, 1024 * 50);
        FileOutputStream fos = new FileOutputStream("/sdcard/" + file);
        byte[] buffer = new byte[1024 * 50];

        int current = 0;
        while ((current = bis.read(buffer)) != -1) {
            fos.write(buffer, 0, current);
        }

        fos.flush();
        fos.close();
        bis.close();

    } catch (IOException e) {
        Log.d("ImageManager", "Error: " + e);
    }

}

上面的代码没有下载音频文件。 如果使用清单文件中的任何权限请告诉我..(我已使用互联网权限) 请帮忙

thanks..


你还必须添加

android.permission.WRITE_EXTERNAL_STORAGE

如果您想将数据写入 SD 卡,请授予权限。

如果您收到任何 IOExceptions,还可以发布您的 logcat 输出。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过 url 从服务器下载音频文件 的相关文章

随机推荐