Folder added on Android is not visible via USB
I faced the same issue and rebooting either the Android device or the PC is not a practical solution for users. :)
This issue is through the use of the MTP protocol (I hate this protocol). You have toinitiate a rescan of the available files, and you can do this using the MediaScannerConnection
class:
// Snippet taken from questionFile path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);path = new File(path, "SubDirName");path.mkdirs();// Initiate a media scan and put the new things into the path array to// make the scanner aware of the location and the files you want to seeMediaScannerConnection.scanFile(this, new String[] {path.toString()}, null, null);
The way used in Baschi's answer doesn't always work for me. Well, here is a full solution.
// Snippet taken from questionFile path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);path = new File(path, "SubDirName");path.mkdirs();// Fixpath.setExecutable(true);path.setReadable(true);path.setWritable(true);// Initiate media scan and put the new things into the path array to// make the scanner aware of the location and the files you want to seeMediaScannerConnection.scanFile(this, new String[] {path.toString()}, null, null);
The only thing that worked for me was this:
Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);Uri fileContentUri = Uri.fromFile(path);mediaScannerIntent.setData(fileContentUri);this.sendBroadcast(mediaScannerIntent);