大家好,我对 DownloadManager 有点困惑,试图为我的示例应用程序添加下载功能。 问题是,当我执行代码下载文件时,什么也没发生,大约15分钟后,显示通知(下载失败)。
这是我使用的代码:
downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("Sample video name.mp4");
Long reference = downloadManager.enqueue(request);
任何帮助将不胜感激。
答案 0 :(得分:1)
使用此功能可通过下载管理器下载文件:
private long downloadFile(Uri uri, String fileStorageDestinationUri, String fileName) {
long downloadReference = 0;
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
try {
DownloadManager.Request request = new DownloadManager.Request(uri);
//Setting title of request
request.setTitle(fileName);
//Setting description of request
request.setDescription("Your file is downloading");
//set notification when download completed
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalPublicDir(fileStorageDestinationUri, fileName);
request.allowScanningByMediaScanner();
//Enqueue download and save the referenceId
downloadReference = downloadManager.enqueue(request);
} catch (IllegalArgumentException e) {
BaseUtils.showToast(mContext, "Download link is broken or not availale for download");
Log.e(TAG, "Line no: 455,Method: downloadFile: Download link is broken");
}
return downloadReference;
}
现在像下面这样调用此方法:
long downloadFileRef = downloadFile(Uri.parse("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"), "/YourAppName/", "fileName.mp4");
if (downloadFileRef != 0) {
BaseUtils.showToast(mContext, "Starting download...");
}else {
BaseUtils.showToast(mContext, "File is not available for download");
}
您可以在设备的Storage/YourAppName/fileName.mp4
中找到下载的文件
答案 1 :(得分:0)
这是我的实现方式:
MATCH p=(n0:I {myId:'1234'})-[:rel1 *0..1]->(n1)-[r2:rel2 *0..1]->(n2)<-[:rel3 *0..1]-(n3)-[:rel4 *0..1]->(n4)
RETURN [n IN NODES(p) WHERE n.myId <> n2.myId | n.myId] AS nodeIds,
[r IN RELATIONSHIPS(p) WHERE r <> r2] AS relations;
然后:
长期私有下载文件(Uri uri,字符串fileStorageDestinationUri,字符串fileName){
public void downloadHD(){
long downloadFileRef = downloadFile(Uri.parse(Video.VideoUrl()), "/"+getString(R.string.app_name)+"/", Video.VideoName()+" HD"+".mp4");
if (downloadFileRef != 0) {
this.showToast(getString(R.string.startdownload));
}else {
this.showToast(getString(R.string.notavialblefordownload));
}
}