[英]Android check if downloaded file exists
我嘗試下載該文件后檢查該文件是否存在,但是對我說它不存在
@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";
}
}
下載過程正在后台進行。 因此,在enqueue()之后,您的文件不存在,因為尚未下載。 您只需要使用該ACTION_DOWNLOAD_COMPLETE意向過濾器注冊BroadcastReceiver。 當下載完成時,DownloadManager將廣播。 請參閱此處的文檔: https : //developer.android.com/reference/android/app/DownloadManager.html#ACTION_DOWNLOAD_COMPLETE
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.