3

My app downloads .mp3 files to download folder. It works fine, but then no music player app is able to play it ("cannot play this type of files").

I have to reboot every time to force the system to run media scanner, to make it work.

I tried MediaScannerConnection, but it does not work on my API level (24)

 File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    MediaScannerConnection.scanFile(this, new String[]{sdCard.getPath()},
            null, new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    // now visible in gallery
                    Toast.makeText(getBaseContext(), "files have been scanned dawg", Toast.LENGTH_LONG).show();
                }
            });

It does not show any toast.

I have found another method, but I have heard that it breaks from android 4.4

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

This method breaks from android 7 apparently

  File file = new File(absolutePath);
   Uri uri = Uri.fromFile(file);
   Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
   sendBroadcast(intent);

How do I make it work on api level 15-current? Is there any universal way? None of them worked on my device yet.

0 Answers0