如何在Windows上使用Java在默认图像查看器中打开图像? [英] How do I open an image in the default image viewer using Java on Windows?
问题描述
我有一个按钮来查看附加到日志条目的图像,当用户单击该按钮时,我希望它在Windows计算机上的用户默认图像查看器中打开图像?
I have a button to view an image attached to a log entry and when the user clicks that button I want it to open the image in the user's default image viewer on a Windows machine?
我如何知道默认图像查看器中的哪个查看器?
How do I know which viewer in the default image viewer?
现在我正在做这样的事情,但它不起作用:
Right now I'm doing something like this but it doesn't work:
String filename = "\""+(String)attachmentsComboBox.getSelectedItem()+"\"";
Runtime.getRuntime().exec("rundll32.exe C:\\WINDOWS\\System32\\shimgvw.dll,ImageView_Fullscreen "+filename);
如果不起作用,我的意思是它什么都不做。我试图在命令行中运行该命令,但没有任何反应。没有错误,没有。
And by doesn't work I mean it doesn't do anything. I tried to run the command just in the command line and nothing happened. No error, nothing.
推荐答案
尝试使用CMD / C START
Try with the 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.");
}
}
这将启动与文件关联的默认照片查看器延期。
This will start the default photo viewer associated with the file extension.
更好的方法是使用java.awt.Desktop。
A better way is to use 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.");
}
}
这篇关于如何在Windows上使用Java在默认图像查看器中打开图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!