fralbo
fralbo

Reputation: 2664

How to use DownloadManager?

There is something I don't understand about Android DownloadManager because if I download a file from Chrome (it's the same when I download a file from my own application) and while the file is being downloaded, if I take a look in sdcard/Download with a file explorer, I can see the file named with it's final name and with it's final size whereas the file is not available again.

So, I wonder how to know when the file is ok or not.

Oh sure, if I'm still running my application, I can just wait to be notified by he Broadcast receiver but that's a situation which could not occur for long time downloads.

If I leave my app and run it a moment after, how to check my downloads?

Upvotes: 0

Views: 382

Answers (1)

Amir
Amir

Reputation: 16607

With Curser you can find status of your file. Your code maybe something like this :

mIdColumnId = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    mTitleColumnId = cursor
        .getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE);
    mStatusColumnId = cursor
        .getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS);
    mReasonColumnId = cursor
        .getColumnIndexOrThrow(DownloadManager.COLUMN_REASON);
    mTotalBytesColumnId = cursor
        .getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
    mCurrentBytesColumnId = cursor
        .getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
    mMediaTypeColumnId = cursor
        .getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE);
    mDateColumnId = cursor
        .getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP);

By dividing TOTAL_SIZE and DOWNLOAD_SO_FAR you can find what percent of file downloaded. for complete code take a look this library.

Upvotes: 1

Related Questions