Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Folder added on Android is not visible via USB

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?

like image 821
eje211 Avatar asked Nov 22 '12 07:11

eje211


People also ask

How do I access files from USB on Android?

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.

Why can't I see my Android phone files on my 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.

How do I make Android files visible in Windows 10?

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.

How can I see hidden files in Android internal storage over USB?

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.


2 Answers

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); 
like image 126
Baschi Avatar answered Sep 20 '22 23:09

Baschi


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); 
like image 20
raziEiL Avatar answered Sep 22 '22 23:09

raziEiL