【问题标题】:need help for android mp3 file downloadandroid mp3 文件下载需要帮助
【发布时间】:2011-04-14 09:54:58
【问题描述】:

我正在开发一个 android 应用程序,我需要下载 mp3 文件

但下载过程应该在后台进行。
有人对此有任何想法吗?

【问题讨论】:

    标签: android file download mp3


    【解决方案1】:

    我终于找到了解决办法。

    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();
    

    }

    它对我很有用。

    【讨论】:

    • 很奇怪。该代码只需稍作调整即可为我工作,但我觉得奇怪的是,它在没有设置任何权限的情况下工作......除了互联网......
    【解决方案2】:

    android: Is it possible to dowload file to disk given an xml (from a URL)?

    检查一下:带有 downloadFileFrom 方法的 AsyncTask 类将帮助您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      相关资源
      最近更新 更多