在我的应用程序中,我想重命名和删除外部存储目录上的文件夹或文件。为此,我使用了来自这个问题的 mediascanner 的修改版本: Android How to use MediaScannerConnection scanFile
这似乎工作正常,因为例如 android 应用程序“AndroZip”显示重命名的文件夹。但在 Windows 资源管理器中,该文件夹仍然具有旧名称,并且没有扩展名的新文件以新文件夹名称出现。我尝试断开 USB 电缆,刷新 Windows 资源管理器或使用其他媒体扫描器实现,如广播意图。但唯一有效的是重新启动我的安卓设备。
媒体扫描仪:
public class SingleMediaScanner implements MediaScannerConnectionClient {
private MediaScannerConnection mediaScannerConnection;
private String path;
SingleMediaScanner(Context context, String path) {
this.path = path;
mediaScannerConnection = new MediaScannerConnection(context, this);
mediaScannerConnection.connect();
}
@Override
public void onMediaScannerConnected()
{
File folder = new File(path);
if (folder.isDirectory())
{
String[] filesInFolder = folder.list();
for (int i = 0; i < filesInFolder.length; i++)
{
mediaScannerConnection.scanFile(path + "/" + filesInFolder[i], null);
}
mediaScannerConnection.scanFile(path, null);
}
else
{
mediaScannerConnection.scanFile(path, null);
}
}
@Override
public void onScanCompleted(String path, Uri uri) {
mediaScannerConnection.disconnect();
}
}
重命名方法
public void renameFormular(String oldName, String newName)
{
File oldFolderName = new File(FORMULARS + "/" + oldName);
File newFolderName = new File(FORMULARS + "/" + newName);
oldFolderName.renameTo(newFolderName);
String[] values = newFolderName.list();
for(int i = 0; i < values.length; i++)
{
String oldFormularFileName = FORMULARS + "/" + newFolderName + "/" + values[i];
String newFormularFileName = oldFormularFileName.replace(oldName, newName);
File oldFormularFile = new File(oldFormularFileName);
File renamedFormularFile = new File(newFormularFileName);
oldFormularFile.renameTo(renamedFormularFile);
}
new FolderMediaScanner(context, FORMULARS);
}