Problem: Wie öffne ich eine Datei, die sich im Bundle/meiner RCP Applikation befindet. In der Beispielanwendung wird eine BMP Datei geöffnet.
Schritt 1: Verzeichnis mit BMP Datei anlegen
Die Datei befindet sich im Rootverzeichnis des Projekts. Bild 1 zeigt eine Projektstruktur mit dem Verzeichnis
myfiles
und einer Bilddatei picture.bmp
Bild 1 |
Schritt 2: Build Configuration anpassen
In der Build Configuration des plugin.xml im Binary Build muss das Verzeichnis mit eingebunden sein. (Bild 2)
Bild 2 |
Der Quellcode kann z.B. in einen Command Handler eingefügt werden. Es wird die Standardapplikation für den jeweiligen Dateityp gestartet. Diese muss vorher im Betriebssystem definiert sein. In Windows ist dies z.B. Paint.
URL fileURL = null;
try {
fileURL = FileLocator
.toFileURL(Activator.getDefault()
.getBundle()
.getEntry("myfiles/picture.bmp"));
String filePath = fileURL.getPath();
File file = new File(filePath);
Desktop.getDesktop().open(file);
} catch (IOException e) {
e.printStackTrace();
}
Ein alternativer Code der die gleiche Wirkung hat wird im Codeache Blog von Hugo Corbucci beschrieben.
Bundle bundle = Activator.getDefault().getBundle();
Path path = new Path("myfiles/picture.bmp"); //$NON-NLS-1$
URL url = FileLocator.find(bundle, path, Collections.EMPTY_MAP);
URL fileUrl = null;
try {
fileUrl = FileLocator.toFileURL(url);
File file = new File(fileUrl.getPath());
Desktop.getDesktop().open(file);
} catch (IOException e) {
e.printStackTrace();
}
Schritt 4: Ausführen. Fertig!