0

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?

Nitin Vats
  • 51
  • 10
  • `uri.setValue` ??? I see no uri instance. Where does it come from? – blackapps Feb 23 '21 at 13:11
  • `catch (Exception e) { e.printStackTrace(); }` If there is a catch you are still executing the following code. Why? Isnt it better to stop then? And how do you inform the user about that catch? – blackapps Feb 23 '21 at 13:12

1 Answers1

0
  1. With ContentValues you only registers in the database(inner) but does not insert the downloaded object itself. After you have configured content value use bitmap compress to save your object itself. uri = database.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)), uri = your file uri

  2. Use only:

contentValues.put(MediaStore.DownloadColumns.IS_PENDING, 1);
contentValues.put(MediaStore.Downloads.TITLE,fileName);

without contentValues.put(MediaStore.DownloadColumns.DISPLAY_NAME, fileName).

After saving with bitmap compress

contentValues.clear();
contentValues.put(MediaStore.DownloadColumns.IS_PENDING, 0);                            contentValues.put(MediaStore.Downloads.DISPLAY_NAME, fileName + ".ext");

then

uri.update(insert, values, null, null);
Ne1zvestnyj
  • 1,391
  • 1
  • 7
  • 25