1
我有一個問題,因爲我能夠將文本保存到RTF文件,並且能夠將圖像插入到該文件中,但是當我保存該文件並再次加載時,圖像不顯示。我曾嘗試使用base64來解決這個問題,但那並不奏效。我能做些什麼來將圖像保存到RTF文件中,並在文件重新打開時顯示它?如何將圖像保存爲RTF格式的文件?
這是我的代碼:
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showOpenDialog(null);
File file = fileChooser.getSelectedFile();
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedImage image = ImageIO.read(file);
image = Scalr.resize(image, 200);
document = (StyledDocument) textPane.getDocument();
javax.swing.text.Style style = document.addStyle("StyleName",
null);
StyleConstants.setIcon(style, new ImageIcon(image));
document.insertString(document.getLength(), "ignored text",
style);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
if (option == JFileChooser.CANCEL_OPTION) {
fileChooser.setVisible(false);
}