[英]Android check if downloaded file exists
I try, to check if this file exists after i downloaded it, but its says to me that is doesnt exist 我尝试下载该文件后检查该文件是否存在,但是对我说它不存在
@Override
public void handleResult(Result result)
{
myResult = result;
dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(result.getText());
DownloadManager.Request request = new DownloadManager.Request(uri);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String nameOfFile = URLUtil.guessFileName(result.getText(),null, MimeTypeMap.getFileExtensionFromUrl(result.getText()));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nameOfFile);
dm.enqueue(request);
String erg = "";
File mPath = new File((Environment.DIRECTORY_DOWNLOADS + "/" + nameOfFile));
if (mPath.getAbsoluteFile().exists()) {
erg = "existiert";
}else
{
erg = "existiert nicht";
}
}
The downloading process is happening on background. 下载过程正在后台进行。 So after enqueue() your file doesn't exist cause it's not downloaded yet.
因此,在enqueue()之后,您的文件不存在,因为尚未下载。 You just need to register BroadcastReceiver with this ACTION_DOWNLOAD_COMPLETE intent filter.
您只需要使用该ACTION_DOWNLOAD_COMPLETE意向过滤器注册BroadcastReceiver。 And DownloadManager will broadcast when downloads complete.
当下载完成时,DownloadManager将广播。 See documentation here: https://developer.android.com/reference/android/app/DownloadManager.html#ACTION_DOWNLOAD_COMPLETE
请参阅此处的文档: https : //developer.android.com/reference/android/app/DownloadManager.html#ACTION_DOWNLOAD_COMPLETE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.