• 5
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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?

      • 2
    • I've also seen this behavior in samsung devices. There is no problem in your code. It is the behavior of Samsung USB drivers. Plugout and Plugin the USB again, the directory will start showing up.
      • 1
    • Thanks for the advice, PC. I thought of that too. This is an ASUS Transformer, though. I tried plugging out and back in and that didn't work.
      • 1
    • if the folders are visible in Android File Manager, then according to me its USB's problem and not your code's issue.
      • 2
    • You have to use getExternalStoragePublicDirectory else it wont be available outside the device.

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);
  • 62
Reply Report
      • 1
    • Thanku very much this is a good worked around ... I feel like killing Google for this. This should be done automatically whenever something is written to disk. I hope this is fixed soon by Google
      • 2
    • I think the answer is that Android cares about the Big Money App makers who want to keep their files private and it's also easier to be more secure...with that said, not surprisingly, the difficulty is passed on to us to absorb because that is the best/easiest for Android

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);
  • 5
Reply Report

If you add the folder to the SD card from the PC directly to the card through the 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 the Android file manager program and then it will be listed in the SD card index when connected to the PC.

  • 0
Reply Report

It's work fine for me.

MediaScannerConnection.scanFile work if there is file in directory (not directory)

private fun checkMTPFolder(f: File, context: Context) {
   if (f.isDirectory) {
      val newFilePath = f.absolutePath + "/tempFIle"
      val newFile = File(newFilePath)
      newFile.mkdir()

      MediaScannerConnection.scanFile(context, arrayOf(newFilePath), null, object : MediaScannerConnection.OnScanCompletedListener {
                override fun onScanCompleted(p0: String?, p1: Uri?) {
                    val removedFile = File(p0)
                    removedFile.delete()
                    MediaScannerConnection.scanFile(context,arrayOf(removedFile.absolutePath), null, null)
                }

            })
   }
}
  • 0
Reply Report

I have solved this problem by toggling the phone setting:

  1. After a directory is created and/or a file saved, change from (MTP) mode to USB (SD card) mode for a moment, wait for the SD card mounting on the PC, so the directory and file will be shown.

  2. Turn back to (MTP) mode again where the last file still shows up.

  3. When re-saving a file, you have to change to USB again to see it.

  • -1
Reply Report

Just create the directory on the PC first, and then copy it over to the SD card/phone storage.

You can either put in the contents into the folder first and copy over or just the folder first. As long as the folder is created from the PC, any content can just be copied directly to internal/external mobile devices. For zipped content, it cannot be directly unzipped and copied over unfortunately; you need to unzip them first.

  • -2
Reply Report