我正在尝试将图片保存在 Android 上的子文件夹中。这是我的一些代码:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
path = new File(path, "SubDirName");
path.mkdirs();
(我尝试使用 getExternalStorageDirectory
而不是 getExternalStoragePublicDirectory
和 Pictures 文件夹而不是 DCIM。)
当设备通过 USB 连接时,我添加的任何子文件夹(包括其内容)都不会显示在 Windows 资源管理器中。不过,它确实会显示在 Android 文件管理器中。
我已尝试在新目录的父级上广播 ACTION_MEDIA_MOUNTED
Intent 。没用。
如果我在 Windows 中添加一个文件,它会显示在 Android 上。如果我通过文件管理器在 Android 上添加一个文件,它会显示在 Windows 中。如果我以编程方式添加文件,它会显示在 Android 文件管理器中,但不会显示在 Windows 资源管理器中。我需要从 Windows 获取它,我不希望最终用户必须手动创建文件夹。
我做错了什么?
最佳答案
我遇到了同样的问题,重启 Android 设备或 PC 对用户来说不是一个实用的解决方案。 :)
这个问题是通过使用 MTP协议(protocol)(我讨厌这个协议(protocol))。你必须
启动对可用文件的重新扫描,您可以使用 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);
关于android - 在 Android 上添加的文件夹通过 USB 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13507789/