问题描述
我编写了一个应用程序来使用 Java/SWT 管理我的音频文件。现在我想将文件从 PC 复制到我的手机,这是三星 galaxy A30,Android 10 设备。当我将手机连接到 PC(Win 10)时,它在资源管理器中的“此 PC”下列为“galaxy A30s”,我可以很好地滚动文件夹和文件。但是,当我打开 SWT DirectoryDialog 时,电话并未列出。 有没有人知道为什么会这样以及如何解决它? 非常感谢。
AudioFilescopy afc = new AudioFilescopy(shell);
if (afc.selectDirectory() != null) {
Cursor waitCursor = shell.getdisplay().getSystemCursor(SWT.CURSOR_WAIT);
shell.setCursor(waitCursor);
afc.copyFiles(plSongs);
shell.setCursor(null);
}
这是课程:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardcopyOption;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;
public class AudioFilescopy {
private Shell shell;
private Logger logger;
private String selDirectory;
public AudioFilescopy(Shell parent) {
logger = java.util.logging.Logger.getLogger(this.getClass().getName());
logger.addHandler(MusicCatalog.fileHandler);
shell = new Shell(parent,SWT.BORDER | SWT.RESIZE | SWT.Close | SWT.MAX | SWT.MIN | SWT.PRIMARY_MODAL);
shell.setLayout(new FillLayout());
}
public String selectDirectory() {
DirectoryDialog dialog = new DirectoryDialog(shell);
dialog.setFilterPath(System.getProperty("user.home") + "\\TransferOrdner");
selDirectory = dialog.open();
return selDirectory;
}
public void copyFiles(String[] plSongs) {
for (int i = 0; i < plSongs.length; i++) {
Path source = Paths.get(plSongs[i]);
Path target = Paths.get(selDirectory,source.getFileName().toString());
// System.out.println("copy " + source + " to " + target);
try {
Files.copy(source,target,StandardcopyOption.REPLACE_EXISTING,StandardcopyOption.copY_ATTRIBUTES);
} catch (IOException e) {
logger.log(Level.SEVERE,e.getMessage(),e);
}
}
}
}
解决方法
使用 FileDialog 而不是 DirectoryDialog。