在android下载带有下载管理器问题的文件

时间:2015-06-01 10:40:18

标签: android file android-download-manager

我正在从特定网址下载文件。该功能与其他URL一起正常工作以下载文件,但是当我尝试从this URL下载歌曲时,代码运行成功,但下载的歌曲大小仅为7 kb。歌曲的实际大小超过12MB。我错过了什么吗?

以下是我下载歌曲的代码。

    private void DownloadTask(String downloadUrl){
    Log.e("downloadUrl==>",downloadUrl+"");

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl));
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setAllowedOverRoaming(false);

    request.setDescription("Downloading "+downloadingSongName  );
    request.setTitle("Music App");
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);

    if(ExternalStorage.isAvailable()){
        // if sd card is available
        request.setDestinationInExternalPublicDir(MusicConstants.FOLDERNAME, downloadingSongName);

        final DownloadManager manager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);

        final long downloadId = manager.enqueue(request);

        showDownloadProgressDialog("Downloading " + downloadingSongName, getActivity());

        new Thread(new Runnable() {

            @Override
            public void run() {

                boolean downloading = true;

                while (downloading) {

                    DownloadManager.Query q = new DownloadManager.Query();
                    q.setFilterById(downloadId);

                    Cursor cursor = manager.query(q);
                    cursor.moveToFirst();
                    int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                    int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                    if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                        downloading = false;
                        dismissDialog();
                    }


                    final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {

                            try {
                                mProgressBar.setProgress((int) dl_progress);
                                double progressValue=dl_progress;
                                txtDevide.setText(dl_progress+"/100");
                                int percentage=(int) ((progressValue*100)/100);
                                txtpercentage.setText(percentage+"%");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });

                    cursor.close();
                }
            }
        }).start();

    }else{

        showMessage(getActivity(), "No Sd card available.");
    }
}

0 个答案:

没有答案