Questioner
Questioner

Reputation: 2541

MediaScannerConnection.scanFile() doesn't show file in Windows Explorer even though file exists on phone

The method createNewFile() returns true and shows the location of the file on my phone. The file shows up in the file manager of my phone but does not in Explorer.

Basically I call createNewFile() on the File object:

File file = new File(path);
try {
        file.createNewFile());

I write to the file and then call:

MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);

This should show it in Windows Explorer and this has happened with another app but it doesn't with this app and I do not see any relevant difference. Furthermore, neither disconnecting and reconnecting the USB cable or restarting my phone causes the file to show up in Explorer.

Upvotes: 4

Views: 2931

Answers (1)

Sayem
Sayem

Reputation: 5011

Try this :

public static void broadCastToMediaScanner(Context context, File file) {

    Uri contentUri = Uri.fromFile(file);
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    mediaScanIntent.setData(contentUri);
    context.sendBroadcast(mediaScanIntent);
}

Upvotes: 5

Related Questions