Home » Android » How to make downloaded file visible in Gallery in Android Q?-Exceptionshub

How to make downloaded file visible in Gallery in Android Q?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I am using download manager to download file like so:

   DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setTitle(createTitle());
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalFilesDir(activity, Environment.DIRECTORY_DOWNLOADS, subFolders + createFileName());
    request.allowScanningByMediaScanner();

After it is downloaded, I am adding it to the media store like so:

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    ContentValues contentValues = new ContentValues();
                    ContentResolver database = activity.getContentResolver();

                    contentValues.put(MediaStore.Downloads.DISPLAY_NAME, displayName);
                    contentValues.put(MediaStore.Downloads.MIME_TYPE, mimeType);
                    contentValues.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + "/subfolder");
                    database.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
                }

I will see the folder created in the gallery but the file always shows up as corrupted.
Can anyone help ?

How to&Answers: