如何在Windows上使用Java在默认图像查看器中打开图像?

我有一个按钮来查看附加到日志条目的图像,当用户单击该按钮时,我希望它在 Windows机器上的用户认图像查看器中打开图像?

如何知道认图像查看器中的哪个查看器?

现在我正在做这样的事情,但它不起作用:

String filename = "\""+(String)attachmentsComboBox.getSelectedItem()+"\"";
Runtime.getRuntime().exec("rundll32.exe C:\\WINDOWS\\System32\\shimgvw.dll,ImageView_Fullscreen "+filename);

并且通过不起作用我的意思是它什么都不做.我试图在命令行中运行该命令,但没有任何反应.没错,没什么.

尝试使用CMD / C START
public class Test2 {
  public static void main(String[] args) throws Exception {
    String fileName = "c:\\temp\\test.bmp";
    String [] commands = {
        "cmd.exe","/c","start","\"DummyTitle\"","\"" + fileName + "\""
    };
    Process p = Runtime.getRuntime().exec(commands);
    p.waitFor();
    System.out.println("Done.");
 }
}

这将启动与文件扩展名关联的认照片查看器.

更好的方法是使用java.awt.Desktop.

import java.awt.Desktop;
import java.io.File;

public class Test2 {
  public static void main(String[] args) throws Exception {
    File f = new File("c:\\temp\\test.bmp");
    Desktop dt = Desktop.getDesktop();
    dt.open(f);
    System.out.println("Done.");
 }
}

Launch the application associated with a file extension

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...