用android下载wma文件

时间:2015-04-30 06:17:02

标签: android

我使用此post

我的下载wma文件的代码:

final DownloadTask downloadTask = new DownloadTask(YourActivity.this);
downloadTask.execute("www.mysite.com/files/myfile.wma");

下载文件,但文件大小为1 KB! 为什么不下载?

但是。请参阅以下代码:

final DownloadTask downloadTask = new DownloadTask(YourActivity.this);
downloadTask.execute("www.mysite.com/files/otherFile.mp3");

下载完整的文件。

我不知道这个问题

更新:

我的代码:

class DownloadAsyncTask extends AsyncTask<DownloadFileViewModel, Integer, Boolean> {
        @Override
        protected Boolean doInBackground(DownloadFileViewModel... params) {
            if (params.length <= 0)
                return false;
            DirectoryManager.createFolder(model.getDirectory());
            DownloadFileViewModel model = params[0];
            InputStream input = null;
            OutputStream output = null;
            HttpURLConnection connection = null;
            try {
                URL url = new URL(model.getUrl());// url = http://apt.zz.mu/sy/Musics/sound.wma
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                // expect HTTP 200 OK, so we don't mistakenly save error report
                // instead of the file
                if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                    _result= "Server returned HTTP " + connection.getResponseCode()
                            + " " + connection.getResponseMessage();
                }

                // this will be useful to display download percentage
                // might be -1: server did not report the length
                int fileLength = connection.getContentLength();

                // download the file
                input = connection.getInputStream();
                output = new FileOutputStream(path);
                byte data[] = new byte[4096];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) {
                    // allow canceling with back button
                    if (isCancelled()) {
                        input.close();
                        return false;
                    }
                    total += count;
                    // publishing the progress....
                    if (fileLength > 0) // only if total length is known
                        publishProgress((int) (total * 100 / fileLength));
                    output.write(data, 0, count);
                }
                return true;
            } catch (Exception e) {
                _result= e.toString();
            } finally {
                try {
                    if (output != null)
                        output.close();
                    if (input != null)
                        input.close();
                } catch (IOException ignored) {
                    _result=ignored.toString();
                }

                if (connection != null)
                    connection.disconnect();
            }
            if (isCancelled()){
                new File(path).delete();
            }
            return false;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            _progressBar.setProgress(values[0]);
        }
}

0 个答案:

没有答案