I am downloading a file from specific URL. The function is working fine with other URL for downloading the file but when I try to download a song from this URL then code runs successfully but downloaded song is just 7 kb in size. Actual size of song is more than 12MB. Am I missing something?
Here is my code to download a song.
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.");
}
}