Hello @RACHED_BKHAIRIA
Probably this is a well known bug of the Windows Driver for the Android MTP protocol, which doesn’t keep his internal database correctly synchronized with the Android File system changes. It works correctly with Linux machines but not with Ms Windows. The defect is already present with Android 4.4 devices
The possible workaround for an application that have to publish some files to be seen through Windows Explorer, is to manually force the Refresh of the MTP database.
On internet you can find several article on that topic.
The first that i found is:
http://tofu0913.blogspot.com/2014/01/force-android-mtp-database-refresh-if.html
Force Android MTP database refresh if file in storage changed
Method 1 .
Old method, not recommended.
sendBroadcast(**new** Intent(Intent.***ACTION_MEDIA_MOUNTED*** ,
Uri.parse(**"file://"** + Environment.getExternalStorageDirectory())));
Force whole SDCard rescan.
Side-effect: Some devices will force close or cause System UI exception.
Method 2.
Send intent to scan specific file, once it had been created.
Intent mediaScanIntent = **new** Intent(Intent.***ACTION_MEDIA_SCANNER_SCAN_FILE*** );
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
You need a for loop or recursive call if your target is a directory.
Method 3.
Similar with method 2, but do your Media scan by yourself.
public class MediaScanner implements MediaScannerConnectionClient {
private MediaScannerConnection mScanner ;
private File mFile ;
public MediaScanner(Context context, File file) {
mFile = file;
mScanner = new MediaScannerConnection(context, this );
mScanner .connect();
}
@Override
public void onMediaScannerConnected() {
mScanner .scanFile(mFile.getAbsolutePath(), null );
}
@Override
public void onScanCompleted(String path, Uri uri) {
mScanner.disconnect();
}
public class YourClass {
...
new MediaScanner(context, file);
...
}
Same as method 2, you need a ‘for’ loop or recursive call if your target is a directory.
Suggest to use Thread or ExecutorService for a better performance.
Some more info can be found also here:
https://developer.android.com/reference/android/media/MediaScannerConnection.html
Instead, if you need to “manually” resolve the issue, you have some options:
-
you can reboot the device
-
you can force a FileSystem Rescan by from setting menu:
Go under setting > apps > (show system apps from the menu) > select Media Storage > Storage -> ClearData. This will force a rescan of the system.
(see https://www.doubletwist.com/help/question/how-do-i-reset-the-android-media-scan-database/)
-
download some apps from Google Play like:
https://play.google.com/store/apps/details?id=com.house.noranuko.mediarescanner&hl=en
https://play.google.com/store/apps/details?id=card.rescan.radeff&hl=it
or many other
-
use and ADB command as described here:
https://android.stackexchange.com/questions/199685/manually-trigger-mediascanner-to-scan-files-on-internal-storage-on-nougat-oreo
Regards
Simone Callegari
Mobile Products L3 Specialist SW Engineer