I'm trying to save pictures in a subfolder in Android. Here's a bit of my code:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
path = new File(path, "SubDirName");
path.mkdirs();
(I've tried getExternalStorageDirectory
instead of getExternalStoragePublicDirectory
and the Pictures folder instead of DCIM.)
The thing is that any subfolder I add, including its contents, don't show up in Windows Explorer when the device is connected via USB. It does show in the Android File Manager, though.
I've tried broadcasting the ACTION_MEDIA_MOUNTED
intent on the new directory's parent. It didn't work.
If I add a file in Windows, it shows up on Android. If I add a file on Android via the File Manager, it shows up in Windows. If I add the file programmatically, it shows up on the Android File Manager but not in Windows Explorer. And I need to get it from Windows and I don't want the final user to have to create the folder manually.
What am I doing wrong?
Even the topic seems to be old. I faced the same issue and rebooting either the Android device or the PC is not practical solution for users. :) This issue is through the use of the MTP protocol (I hate this protocol). What you have to do is to initiate a rescan of the available files and you can do this using the MediaScannerConnection
class:
// snippet taken from question
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
path = new File(path, "SubDirName");
path.mkdirs();
// 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 see
MediaScannerConnection.scanFile(this, new String[] {path.toString()}, null, null);
This way don't work sometimes for me. Well here is full solution.
// snippet taken from question
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
path = new File(path, "SubDirName");
path.mkdirs();
// fix
path.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 see
MediaScannerConnection.scanFile(this, new String[] {path.toString()}, null, null);
The only thing that worked for me is this:
Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileContentUri = Uri.fromFile(path);
mediaScannerIntent.setData(fileContentUri);
this.sendBroadcast(mediaScannerIntent);
Credit to https://stackoverflow.com/a/12821924/1964666
if you add the folder to SD card from PC directly to the card through card reader it will not show in windows explorer when connected with the phone, the solution is to copy or move the same folder using Android file manager program and then it will be listed in the SD card index when connected to PC.
I have solved this problem by toggle the phone setting:
1) After Dir created and/or file saved, Chang from (MTP) mode to USB (SD Card) mode for a moment, wait for sd card mounting to pc, so Dir & file will be shown.
2) Turn back to (MTP) mode again where the last file still show up.
3) when re-save a file u have to change to USB again to see it.
just create the dir on the pc first then copy it over to the sd card/phone storage.
you can either put in the contents into folder first and copy over or just the folder first. long as the folder is created from pc, any content can just be copied directly to internal/external mobile devices. for zipped contents they cannot be directly unzipped and copied over unfortunately, you need to unzip them first.
good luck and have a good day! :)