Search code examples
javaswtrcp

How to load image to view in RCP?


I am developing an RCP plugin project that includes certain views.First view take employee details like name ,address etc.There is an option to upload employee image using browse button.Second view shows the details that have entered in the first view.All details except photo is displaying fine.

It shows a red square in the place of photo label. My code for setting photo is shown like this :

 Label photoLabel = new Label(parent, SWT.NONE);
 photoLabel.setBounds(420, 233, 100, 106);           

 photoLabel.setImage(SWTResourceManager.getImage(FormDataViewClass.class,photoUploadPath));

where photoUploadPath is string variable that contains the path of uploaded photo. How can I solve this issue?


Solution

  • Following code segment helped me to resolve the above problem.

    byte[] uploadedImg = null;
    try {
        File f1 = new File(photoUploadPath);
        double fileLen = f1.length();
        uploadedImg = new byte[(int) fileLen];
        FileInputStream inputStream = new FileInputStream(photoUploadPath);
        int nRead = 0;
        while ((nRead = inputStream.read(uploadedImg)) != -1) {
        System.out.println("!!!!!!!!!!!!!!!!!" + new String(uploadedImg));
        }
        inputStream.close();
    
    } catch (Exception e2) {
        // TODO: handle exception
    }
    
    BufferedInputStream inputStreamReader = new BufferedInputStream(new ByteArrayInputStream(uploadedImg));
    ImageData imageData = new ImageData(inputStreamReader);
    Image image = new Image(Display.getCurrent(), imageData);
    photoLabel.setImage(image);