However, when I try to download to the external sdcard, then status doesn't update until after 2~3 minutes. (Meaning I get 0 bytes downloaded from cursor.getLong(cursor .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));). It eventually downloads after 2~3 minutes.
The notification status also says 0%
private void addToDownloadManager(String sourcePath, String destFolder, String deskFileName, DownloadManager downloadManager) {
try {
if(sourcePath == null || sourcePath.equals(""))
return;
try {
File folder = new File(destFolder);
if (!folder.exists()) {
folder.mkdirs();
}
}catch (Exception e) {
}
Uri Download_Uri = Uri.parse(sourcePath);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
request.setTitle("title");
request.setDescription("description");
File destination = new File(destFolder, deskFileName);
request.setDestinationUri(Uri.fromFile(destination));
downloadReference = downloadManager.enqueue(request);
} catch (Exception e) {
}
}
private Timer mDownloadStatusTimer;
public void downloadStatusTimerSchedule() {
if (mDownloadStatusTimer != null)
downloadStatusTimerCancel();
try {
mDownloadStatusTimer = new Timer("DownloadStatusTimer");
DownloadStatusTimer timer = new DownloadStatusTimer();
mDownloadStatusTimer.schedule(timer, 500, 500); // 0.5 second
} catch (Exception e) {
}
}
public void downloadStatusTimerCancel() {
if (mDownloadStatusTimer != null) {
mDownloadStatusTimer.cancel();
mDownloadStatusTimer.purge();
mDownloadStatusTimer = null;
}
}
private long bytes_downloaded = 0;
private long bytes_total = 0;
public class DownloadStatusTimer extends TimerTask {
@Override
public void run() {
if (mDownloadManager != null) {
DownloadManager.Query myDownloadQuery = new DownloadManager.Query();
Cursor cursor = mDownloadManager.query(myDownloadQuery);
bytes_downloaded = 0;
bytes_total = 0;
try {
if (cursor != null && cursor.moveToFirst()) {
try {
// Get downloaded size/total size
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL ||
cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) {
// do nothing
} else {
bytes_downloaded += cursor.getLong(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
bytes_total += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
}
} catch (Exception e) {
}
while (cursor != null && cursor.moveToNext()) {
try {
// Get downloaded size/total size
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL ||
cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) {
// do nothing
} else {
bytes_downloaded += cursor.getLong(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
bytes_total += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
}
} catch (Exception e) {
}
}
} else {
}
} catch (Exception e) {
} finally {
if (cursor != null) {
cursor.close();
}
}
Log.e("test", "Download size: " + bytes_downloaded + " / " + bytes_total);
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
try {
tv_download_status.setText("Download size: " + bytes_downloaded + " / " + bytes_total);
} catch (Exception e) {
}
}
});
}
}
}
I tested Note 4 and Galaxy S5 and they seems to be fine.
Could it be Android 6.0 thing? or S7? Is it a bug? Or is there anything I am doing wrong here?
If you want to test it yourself, here's the project with source code: //drive.google.com/open?id=0BygTefPD845LTkp5QU1mOHRkMDQ
Here's APK to test: //drive.google.com/open?id=0By...Ec1ZHk5ZWRkWWc
[Edit]
My destination location is: /storage/806E-1A11/Android/data/com.joshua.externalsddownloadtest/files/download/video.mp4
Source is: //downloads.4ksamples.com/downloads/[2160p]%204K-HD.Club-2013-Taipei%20101%20Fireworks%20Trailer%20(4ksamples.com).mp4