I'm downloading all files in my application using DownloadManager. Also, I save all id's which enqueue method returns. The question is: How to determine is file still exist after some amount of time?
I'm trying to do this in that way
Cursor c =
mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
if (c == null || !c.moveToFirst()) {
// File doesn't exist?
}
But that didn't work. Sometimes downloadManager returns a Cursor and moveToFirst invocation return true but in fact, downloaded file was been deleted from a filesystem.
Okay, second way I found - obtain a content Uri from the download manager. Also, I'm reading this post about content Uri limitations. If I understand correctly, in short - Uri doesn't guarantee that content exists, furthermore, Uri even doesn't guarantee that content IS A file. Anyway, I think that this construction should work:
try {
contentResolver.openFileDescriptor(contentUri, "r");
} catch (FileNotFoundException e) {
// File doesn't exist?
}
But the result is very frustrating. Sometimes (absolutely random) application crash with SecurityException.
java.lang.SecurityException: Permission Denial: reading com.android.providers.downloads.DownloadProvider uri content://downloads/all_downloads/1145 from pid=16585, uid=10086 requires android.permission.ACCESS_ALL_DOWNLOADS, or grantUriPermission()
My questions are: How I can check if downloaded file exists? Why application crashes sometimes? It's looks like heisenbug because behavior is absolutely random.
Have you add permission on your manifest for read or write file.
or if you are working on version 23 then you should use security permission condition before fetching data.