createNewFile() 方法返回 true 并显示文件在我手机上的位置。该文件显示在我手机的文件管理器中,但未显示在资源管理器中。
基本上我在 File 对象上调用 createNewFile():
File file = new File(path);
try {
file.createNewFile());
我写入文件然后调用:
MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);
这应该会在 Windows 资源管理器中显示,这在另一个应用程序中发生过,但在这个应用程序中没有,我没有看到任何相关差异。此外,断开和重新连接 USB 数据线或重启我的手机都不会导致文件显示在资源管理器中。
最佳答案
试试这个:
public static void broadCastToMediaScanner(Context context, File file) {
Uri contentUri = Uri.fromFile(file);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
关于android - 即使手机上存在文件,MediaScannerConnection.scanFile() 也不会在 Windows 资源管理器中显示文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36446862/