I'm trying to save pictures in a subfolder on 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.)
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?
With a USB cable, connect your phone to your computer. On your phone, tap the "Charging this device via USB" notification. Under "Use USB for," select File Transfer. An Android File Transfer window will open on your computer.
Unlock your phone and go to Settings > System > Developer options. Right there, scroll down and look for Default USB configuration, then tap it. Now choose File Transfer or Your Android will be connected as a media device to the computer whenever it's unlocked.
To find your Android phone files on a PC, connect your phone to your PC with a USB cable, and tap the USB notification on your phone. Click the notification, and select Transfer files (MTP) or File Transfer. Your phone will show up on your PC, with the files available for access.
Click on the View tab at the top. You'll see a lot of options under Advanced settings in this tab. 4) Under Advanced settings, select the Show hidden files and folders option and click OK at the bottom of the pop-up window.
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 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 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 see MediaScannerConnection.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 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With