通过http://下载音频文件

时间:2016-08-12 19:48:21

标签: android

我需要下载我网站的音频文件。我怎样才能做到这一点? 我需要下载mp3音频文件并在我的通知中播放。 我曾在各个地方寻求帮助,却一无所获。

我尝试使用此代码,但没有成功,我需要做的另一件事是下载音频,但我无法保存SDCARD,因为在我的应用程序中每小时都有通知

static void downloadFile(String dwnload_file_path, String fileName,
                         String pathToSave) {
        int downloadedSize = 0;
        int totalSize = 0;

        try {
                URL url = new URL(dwnload_file_path);
                HttpURLConnection urlConnection = (HttpURLConnection) url
                                                  .openConnection();

                urlConnection.setRequestMethod("POST");
                urlConnection.setDoOutput(true);

                // connect
                urlConnection.connect();

                File myDir;
                myDir = new File(pathToSave);
                myDir.mkdirs();

                // create a new file, to save the downloaded file

                String mFileName = fileName;
                File file = new File(myDir, mFileName);

                FileOutputStream fileOutput = new FileOutputStream(file);

                // Stream used for reading the data from the internet
                InputStream inputStream = urlConnection.getInputStream();

                // this is the total size of the file which we are downloading
                totalSize = urlConnection.getContentLength();

                // runOnUiThread(new Runnable() {
                // public void run() {
                // pb.setMax(totalSize);
                // }
                // });

                // create a buffer...
                byte[] buffer = new byte[1024];
                int bufferLength = 0;

                while ((bufferLength = inputStream.read(buffer)) > 0) {
                        fileOutput.write(buffer, 0, bufferLength);
                        downloadedSize += bufferLength;
                        // update the progressbar //
                        // runOnUiThread(new Runnable() {
                        // public void run() {
                        // pb.setProgress(downloadedSize);
                        // float per = ((float)downloadedSize/totalSize) * 100;
                        // cur_val.setText("Downloaded " + downloadedSize + "KB / " +
                        // totalSize + "KB (" + (int)per + "%)" );
                        // }
                        // });
                }
                // close the output stream when complete //
                fileOutput.close();
                // runOnUiThread(new Runnable() {
                // public void run() {
                // // pb.dismiss(); // if you want close it..
                // }
                // });

        } catch (final MalformedURLException e) {
                // showError("Error : MalformedURLException " + e);
                e.printStackTrace();
        } catch (final IOException e) {
                // showError("Error : IOException " + e);
                e.printStackTrace();
        } catch (final Exception e) {
                // showError("Error : Please check your internet connection " + e);
        }
}

0 个答案:

没有答案