我需要一些帮助:)。 我有这样的代码:
public void downloadFile(Uri uri, String title, String description, String subDir) {
try {
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDescription(description).setTitle(title);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
//request.setVisibleInDownloadsUi(false); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalFilesDir(DownloadDatabaseActivity.this, Environment.DIRECTORY_DOWNLOADS, subDir);
this._bDatabaseMngr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
_bDatabaseMngrId = this._bDatabaseMngr.enqueue(request);
/*** HERE IS MY QUESTION - BEGIN ***/
Log.v(TAG, "File size (HTTP Content-length header): " + HOW_TO_GET_CONTENT_LENGTH_HEADER_FROM_DOWNLOADMANAGER);
if(HOW_TO_GET_CONTENT_LENGTH_HEADER_FROM_DOWNLOADMANAGER < getFreeSpace()) {
// I'd like to handle it ;)
}
/*** HERE IS MY QUESTION - END ***/
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
我想要实现的是来自HTTP标头的内容长度值?任何想法(min api level 16)?
答案 0 :(得分:0)
您可以检查下载ID的DownloadManager状态和错误。
DownloadManager.ACTION_DOWNLOAD_COMPLETE
检查onReceive
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(id);
Cursor cursor = downloadManager.query(query);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
}
您可以找到所有状态代码here