带有DownloadManager的Android获取文件总大小 [英] Android with DownloadManager get total file size
问题描述
我研究了所有地方,但没有任何解决方案。我有一个片段代码,可以在Eclipse的调试模式下正常运行并获取文件的总大小。但是,当我运行该项目时,sizeOfDownloadingFile的值仅返回-1。
I have researched every where but no any solution. I have a snippet code that run and get the total file size normally while in debug mode of eclipse. But when i run the project the value of sizeOfDownloadingFile only return -1. Please help me, I compile with Android 22, thanks in advance.
long id = downloadManager.enqueue(request);
Cursor cursor = downloadManager.query(new DownloadManager.Query()
.setFilterById(id));
if (!cursor.moveToFirst()) {
Log.v("DownloadManagerService", "download list is empty");
return;
}
int sizeOfDownloadingFile = 0;
sizeOfDownloadingFile = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
Log.d("DownloadManagerService",
"File size of film " + mFilm.getmTitle() + " is "
+ sizeOfDownloadingFile);
推荐答案
如果在主线程中调用代码,请尝试:
If you call your code in main thread try:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
check here size of your downloading file
}
},500);
在调试模式下,下载器可能会开始下载并且您具有有效值,但是如果您运行了应用正常下载器必须连接到服务器并下载文件,这有时比执行代码花费更多的时间。
When you are in debug mode downloader propably start download and you have a valid value, but if you ran app normaly downloader have to connect to server and doanload file, this sometimes take more time than execution of your code.
这篇关于带有DownloadManager的Android获取文件总大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!