The problem with my code is, it is downloading the file and it is showing in the gallery having path like => "/storage/emulated/0/Download/filename.jpg"
But when I click on the finished download notification it opens a file with a URI => "content://com.android.providers.downloads.documents/document/7123"
and it is of 0kb.
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//Setting title of request
request.setTitle(fileName);
request.setAllowedOverRoaming(true);
request.allowScanningByMediaScanner();
String mimeType =
MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
request.setAllowedOverMetered(true);
if (!Utils.checkNullOrblank(mimeType))
request.setMimeType(mimeType);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
{
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
}else
request.setDestinationInExternalPublicDir(filepath, fileName);
downloadId = downloadManager.enqueue(request);
} catch (Exception e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Downloads.TITLE,fileName);
contentValues.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.Downloads.MIME_TYPE, mimeType);
contentValues.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
// Insert into the database
ContentResolver database = context.getContentResolver();
uri.setValue(database.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues));
}
what am i doing wrong? how to do this process in a correct way?