我有一个button来查看附加到日志条目的图像,当用户单击该button时,我希望它打开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."); } }
请参阅启动与文件扩展名关联的应用程序
您可以使用桌面类完全按照您的需要打开与系统相关的应用程序。
File file = new File( fileName ); Desktop.getDesktop().open( file );
另一种在Windows XP / Vista / 7上运行良好的解决方案,可以打开任何类型的文件(url,doc,xml,image等)
Process p; try { String command = "rundll32 url.dll,FileProtocolHandler \""+ new File(filename).getAbsolutePath() +"\""; p = Runtime.getRuntime().exec(command); p.waitFor(); } catch (IOException e) { // TODO Auto-generated catch block } catch (InterruptedException e) { // TODO Auto-generated catch block }