Folder added on Android is not visible via USB

后端 未结 8 926
青春惊慌失措
青春惊慌失措 2020-11-30 01:18

I\'m trying to save pictures in a subfolder on Android. Here\'s a bit of my code:

File path = Environment.getExternalStoragePublicDirectory(Environment.DIREC         


        
相关标签:
8条回答
  • 2020-11-30 01:35

    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);
    
    0 讨论(0)
  • 2020-11-30 01:36

    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.

    0 讨论(0)
  • 2020-11-30 01:38

    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 讨论(0)
  • 2020-11-30 01:42

    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);
    
    0 讨论(0)
  • 2020-11-30 01:44

    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 讨论(0)
  • 2020-11-30 01:46

    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.

    0 讨论(0)
提交回复
热议问题