fieldgroup, ds, embeeded, base64

Hello some body can check my error, i am trying to send over my field who’s have my BASE64 String image but I do not know if using the field group have the string but when i execute the DpiEdit.jjava show me the error…


package ezmovil.net.dpitest.web.dpi;
import com.haulmont.cuba.core.app.FileStorageService;
import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.FileStorageException;
import com.haulmont.cuba.gui.components.AbstractEditor;
import com.haulmont.cuba.gui.components.Embedded;
import com.haulmont.cuba.gui.components.TextField;
import com.haulmont.cuba.gui.data.Datasource;
import ezmovil.net.dpitest.entity.Dpi;

import javax.inject.Inject;
import javax.inject.Named;

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Map;

import static com.haulmont.cuba.gui.components.KeyCombination.Key.R;

public class DpiEdit extends AbstractEditor<Dpi> {

    final Charset UTF8_CHARSET = Charset.forName("UTF-8");

    @Inject
    private Datasource<Dpi> dpiDs;
    @Inject
    private Embedded embedded;

    @Named("dpiFieldGroup.base64foto")
    private TextField base64fotoField;

    @Override
    public void init(Map<String, Object> params) {
        showEmbeeded();
    }
    private void showEmbeeded(){

        String base64AsString = base64fotoField.getValue();

        byte[] imageAsBytes = null;

        imageAsBytes = base64AsString.getBytes();
        imageAsBytes = dpiDs.getItem().getBase64foto().getBytes();

        //String encoded = java.util.Base64.getEncoder().encodeToString(stringAsBytes);

        if (imageAsBytes != null) {
            showNotification("Visible true.", NotificationType.HUMANIZED);
            embedded.setVisible(true);
            //embedded.setSource("foto.jpg", new ByteArrayInputStream(imageAsBytes));
            //embedded.setType(Embedded.Type.IMAGE);
        } else {
            showNotification("Visible false.", NotificationType.HUMANIZED);
            embedded.setVisible(false);
        }
    }

    String decodeUTF8(byte[] bytes){
        return new String(bytes, UTF8_CHARSET);
    }

    byte[] encodeUTF8(String string){
        return string.getBytes(UTF8_CHARSET);
    }
}

layout>
        <hbox spacing="true">
            <scrollBox id="scrollBox"
                       height="201px">
                <fieldGroup id="dpiFieldGroup"
                            border="visible"
                            datasource="dpiDs"
                            editable="false"
                            height="200px">
                    <column width="250px">
                        <field id="cui"/>
                        <field id="name1"/>
                        <field id="name2"/>
                        <field id="last1"/>
                        <field id="last2"/>
                        <field id="lastmarried"/>
                        <field id="genero"/>
                        <field id="municipio"/>
                        <field id="depto"/>
                        <field id="pais"/>
                        <field id="fecnac"/>
                        <field id="emision"/>
                        <field id="vencimiento"/>
                        <field id="estadocivil"/>
                        <field id="municipioDpi"/>
                        <field id="deptoDpi"/>
                        <field id="paisDpi"/>
                        <field id="lee"/>
                        <field id="escribe"/>
                        <field id="libro"/>
                        <field id="folio"/>
                        <field id="partida"/>
                        <field id="mrz2"/>
                        <field id="base64foto"/>
                        <field id="pathFoto"/>
                        <field id="base64Firma"/>
                        <field id="pathFirma"/>
                    </column>
                </fieldGroup>
            </scrollBox>
            <embedded id="embedded"
                      height="200px"
                      width="200px"/>
        </hbox>

Hello, i review my code and now its getting my field, but does not convert to IMAGE, some body can help me to understand how works the embeeded control/image?

public class DpiEdit extends AbstractEditor {

@Inject
private Datasource<Dpi> dpiDs;
@Inject
private Embedded embedded;

@Inject
private FieldGroup dpiFieldGroup;

@Named("dpiFieldGroup.base64Firma")
private TextField base64FirmaField;

@Override
public void init(Map<String, Object> params) {

    String foto = dpiFieldGroup.getField("base64foto").toString();
    showEmbeeded(foto);
}
private void showEmbeeded(String foto){

    byte[] imageAsBytes = null;

    imageAsBytes = foto.getBytes();
    byte[] decoded = Base64.getDecoder().decode(imageAsBytes);

    if (imageAsBytes != null) {
        showMessageDialog("embeeded",foto.getBytes().toString().substring(0, 10).toString(),
                MessageType.CONFIRMATION);
        embedded.setVisible(true);
        embedded.setSource("foto.jpg", new ByteArrayInputStream(decoded));
        embedded.setType(Embedded.Type.IMAGE);
    } else {
        showNotification("Visible false.", NotificationType.HUMANIZED);
        embedded.setVisible(false);
    }
}

String decodeUTF8(byte[] bytes){
    return new String(bytes, UTF8_CHARSET);
}

byte[] encodeUTF8(String string){
    return string.getBytes(UTF8_CHARSET);
}

}

Hello i resolve finally, here the code.

@Override
public void postInit() {

    showEmbeeded();
}
private void showEmbeeded(){

    String strImage = dpiFieldGroup.getDatasource().getItem().getValue("base64foto");

    byte[] imageAsBytes = strImage.getBytes();
    byte[] decoded = Base64.getMimeDecoder().decode(strImage);

    //showMessageDialog("dpiFieldGroup", strImage,
    //        MessageType.CONFIRMATION);

    if (imageAsBytes != null) {
        embedded.setVisible(true);
        embedded.setSource("fotoDPI.jpg", new ByteArrayInputStream(decoded));
        //embedded.setSource(decodeUTF8(imageAsBytes));
        embedded.setType(Embedded.Type.IMAGE);
    } else {
        showNotification("Visible false.", NotificationType.HUMANIZED);
        embedded.setVisible(false);
    }
}

String decodeUTF8(byte[] bytes){
    return new String(bytes, UTF8_CHARSET);
}

byte[] encodeUTF8(String string){
    return string.getBytes(UTF8_CHARSET);
}

}

cuba-fieldgroup-embeeded-base64-image