在android中添加的文件夹不能通过USB看到

eje*_*211 34 filesystems android

我正在尝试将图片保存在Android的子文件夹中.这是我的一些代码:

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
path = new File(path, "SubDirName");
path.mkdirs();
Run Code Online (Sandbox Code Playgroud)

(我尝试过getExternalStorageDirectory而不是getExternalStoragePublicDirectory和图片文件夹而不是DCIM.)

问题是,当通过USB连接设备时,我添加的任何子文件夹(包括其内容)都不会显示在Windows资源管理器中.但它确实在Android文件管理器中显示.

我试过ACTION_MEDIA_MOUNTED在新目录的父目录上广播意图.它没用.

如果我在Windows中添加文件,它将显示在Android上.如果我通过文件管理器在Android上添加文件,它将显示在Windows中.如果我以编程方式添加文件,它将显示在Android文件管理器上,但不会显示在Windows资源管理器中.我需要从Windows获取它,我不希望最终用户必须手动创建该文件夹.

我究竟做错了什么?

Bas*_*chi 60

甚至这个话题似乎都很老了.我遇到了同样的问题,重新启动Android设备或PC对用户来说并不实用.:)这个问题是通过使用MTP协议(我讨厌这个协议).您需要做的是启动可用文件的重新扫描,您可以使用以下MediaScannerConnection类来执行此操作:

// 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);
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢这是一个很好的解决方案......我觉得要为此杀死谷歌.只要将某些内容写入磁盘,就应该自动执行此操作.我希望谷歌能尽快解决这个问题 (8认同)

小智 5

Baschi答案中使用的方式并不总是对我有用。好吧,这是一个完整的解决方案。

// 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);
Run Code Online (Sandbox Code Playgroud)


Dan*_*hev 1

以上都没有帮助我,但这有效:技巧是不扫描新文件夹,而是在新文件夹中创建一个文件,然后扫描该文件。现在,Windows 资源管理器将新文件夹视为真正的文件夹。

    private static void fixUsbVisibleFolder(Context context, File folder) {
    if (!folder.exists()) {
        folder.mkdir();
        try {
            File file = new File(folder, "service.tmp");//workaround for folder to be visible via USB
            file.createNewFile();
            MediaScannerConnection.scanFile(context,
                    new String[]{file.toString()},
                    null, (path, uri) -> {
                        file.delete();
                        MediaScannerConnection.scanFile(context,
                                new String[]{file.toString()} ,
                                null, null);
                    });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

感谢https://issuetracker.google.com/issues/37071807#comment90

您还应该类似地扫描目录中每个创建的文件:

   private static void fixUsbVisibleFile(Context context, File file) {
    MediaScannerConnection.scanFile(context,
            new String[]{file.toString()},
            null, null);
}
Run Code Online (Sandbox Code Playgroud)