Java error when inserting image into worksheet. Apache Poi

1

I'm trying to insert an image into a sheet using Apache POI, but I'm getting the following error:

03-03 20:21:50.898: E/SELinux(28413): selinux_android_seapp_context_reload: Error reading /seapp_contexts, line 16, name levelFrom, value container
03-03 20:21:50.908: D/dalvikvm(28413): Late-enabling CheckJNI
03-03 20:21:51.668: D/libEGL(28413): loaded /vendor/lib/egl/libEGL_adreno.so
03-03 20:21:51.678: D/libEGL(28413): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
03-03 20:21:51.708: D/libEGL(28413): loaded /vendor/lib/egl/libGLESv2_adreno.so
03-03 20:21:51.718: I/Adreno-EGL(28413): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_3.2_RB3.04.03.00.134.050_msm8610_JB_3.2_RB3__release_AU ()
03-03 20:21:51.718: I/Adreno-EGL(28413): OpenGL ES Shader Compiler Version: 20.00.02
03-03 20:21:51.718: I/Adreno-EGL(28413): Build Date: 11/12/13 Tue
03-03 20:21:51.718: I/Adreno-EGL(28413): Local Branch: 
03-03 20:21:51.718: I/Adreno-EGL(28413): Remote Branch: quic/jb_3.2_rb3.21
03-03 20:21:51.718: I/Adreno-EGL(28413): Local Patches: NONE
03-03 20:21:51.718: I/Adreno-EGL(28413): Reconstruct Branch: AU_LINUX_ANDROID_JB_3.2_RB3.04.03.00.134.050 + NOTHING
03-03 20:21:51.978: D/OpenGLRenderer(28413): Enabling debug mode 0
03-03 20:21:52.518: W/IInputConnectionWrapper(28413): getExtractedText on inactive InputConnection
03-03 20:21:52.528: W/IInputConnectionWrapper(28413): getTextBeforeCursor on inactive InputConnection
03-03 20:21:52.538: W/IInputConnectionWrapper(28413): getSelectedText on inactive InputConnection
03-03 20:21:52.538: W/IInputConnectionWrapper(28413): getTextAfterCursor on inactive InputConnection
03-03 20:21:58.468: E/SpannableStringBuilder(28413): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-03 20:21:58.468: E/SpannableStringBuilder(28413): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-03 20:21:58.978: E/SpannableStringBuilder(28413): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-03 20:21:58.978: E/SpannableStringBuilder(28413): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-03 20:22:02.368: D/dalvikvm(28413): GC_FOR_ALLOC freed 235K, 11% free 9609K/10696K, paused 15ms, total 15ms
03-03 20:22:02.418: D/dalvikvm(28413): GC_FOR_ALLOC freed 11K, 9% free 10061K/10972K, paused 13ms, total 13ms
03-03 20:22:02.458: D/dalvikvm(28413): GC_FOR_ALLOC freed 9K, 8% free 10532K/11424K, paused 15ms, total 15ms
03-03 20:22:02.928: D/dalvikvm(28413): GC_FOR_ALLOC freed 734K, 13% free 11166K/12760K, paused 24ms, total 24ms
03-03 20:22:03.398: W/dalvikvm(28413): Exception Ljava/lang/RuntimeException; thrown while initializing Lorg/apache/poi/ddf/DefaultEscherRecordFactory;
03-03 20:22:03.398: D/AndroidRuntime(28413): Shutting down VM
03-03 20:22:03.398: W/dalvikvm(28413): threadid=1: thread exiting with uncaught exception (group=0x41d23898)
03-03 20:22:03.428: E/AndroidRuntime(28413): FATAL EXCEPTION: main
03-03 20:22:03.428: E/AndroidRuntime(28413): java.lang.IllegalStateException: Could not execute method of the activity
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.view.View$1.onClick(View.java:3839)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.view.View.performClick(View.java:4476)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.view.View$PerformClick.run(View.java:18795)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.os.Handler.handleCallback(Handler.java:730)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.os.Looper.loop(Looper.java:176)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.app.ActivityThread.main(ActivityThread.java:5493)
03-03 20:22:03.428: E/AndroidRuntime(28413): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 20:22:03.428: E/AndroidRuntime(28413): at java.lang.reflect.Method.invoke(Method.java:525)
03-03 20:22:03.428: E/AndroidRuntime(28413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
03-03 20:22:03.428: E/AndroidRuntime(28413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
03-03 20:22:03.428: E/AndroidRuntime(28413): at dalvik.system.NativeStart.main(Native Method)
03-03 20:22:03.428: E/AndroidRuntime(28413): Caused by: java.lang.reflect.InvocationTargetException
03-03 20:22:03.428: E/AndroidRuntime(28413): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 20:22:03.428: E/AndroidRuntime(28413): at java.lang.reflect.Method.invoke(Method.java:525)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.view.View$1.onClick(View.java:3834)
03-03 20:22:03.428: E/AndroidRuntime(28413): ... 11 more
03-03 20:22:03.428: E/AndroidRuntime(28413): Caused by: java.lang.ExceptionInInitializerError
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.record.AbstractEscherHolderRecord.convertToEscherRecords(AbstractEscherHolderRecord.java:78)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.record.AbstractEscherHolderRecord.convertRawBytesToEscherRecords(AbstractEscherHolderRecord.java:73)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.record.DrawingGroupRecord.processChildRecords(DrawingGroupRecord.java:79)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.model.InternalWorkbook.findDrawingGroup(InternalWorkbook.java:2051)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.usermodel.HSSFWorkbook.initDrawings(HSSFWorkbook.java:1588)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture(HSSFWorkbook.java:1608)
03-03 20:22:03.428: E/AndroidRuntime(28413): at pro.kondratev.androidreadxlsx.ReadXlsx.criar(ReadXlsx.java:136)
03-03 20:22:03.428: E/AndroidRuntime(28413): ... 14 more
03-03 20:22:03.428: E/AndroidRuntime(28413): Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: RECORD_ID
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.ddf.DefaultEscherRecordFactory.recordsToMap(DefaultEscherRecordFactory.java:135)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.ddf.DefaultEscherRecordFactory.<clinit>(DefaultEscherRecordFactory.java:42)
03-03 20:22:03.428: E/AndroidRuntime(28413): ... 21 more
03-03 20:22:03.428: E/AndroidRuntime(28413): Caused by: java.lang.NoSuchFieldException: RECORD_ID
03-03 20:22:03.428: E/AndroidRuntime(28413): at java.lang.Class.getField(Class.java:673)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.ddf.DefaultEscherRecordFactory.recordsToMap(DefaultEscherRecordFactory.java:129)
03-03 20:22:03.428: E/AndroidRuntime(28413): ... 22 more

The Code is:

public void criar (View view) throws IOException {

    String nome = ((String) txtnome.getText().toString());
    String foto = ((String) txtstring.getText().toString());

    FileInputStream input_document = new FileInputStream(new File("sdcard/projetos/enguelber/formulario.xls"));
    HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document); 
    HSSFSheet sheet = my_xls_workbook.getSheetAt(0); 

    // Nome do cliente
    Row row = sheet.getRow(7);
    Cell cell = row.getCell(1);
    String cliente = ((String) txtcliente.getText().toString());

    cell.setCellValue(cliente); 

    // CPF do cliente
    Row row1 = sheet.getRow(7);
    Cell cell1 = row.getCell(7);
    String cpf = ((String) txtcpf.getText().toString());

        cell1.setCellValue(cpf); 

     // Hora

        String hora = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss",
                Locale.getDefault()).format(new Date());
        Row row2 = sheet.getRow(35);

        Cell cell2 = row2.getCell(1);
        cell2.setCellValue(hora);

        // Foto do relatorio

        InputStream inputStream = new FileInputStream("sdcard/projetos/enguelber/fotos/02.03.2016 21.15.27.jpg");

        byte[] bytes = IOUtils.toByteArray(inputStream);

        int pictureIdx = my_xls_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);

        inputStream.close();

        CreationHelper helper = my_xls_workbook.getCreationHelper();

        Drawing drawing = sheet.createDrawingPatriarch();

        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(1);
        anchor.setRow1(2);

        Picture pict = drawing.createPicture(anchor, pictureIdx);

        pict.resize();

       // foto
      //  Row row3 = sheet.getRow(13);
   //     Cell cell3 = row3.getCell(1);

     //   cell3.setCellValue(foto); 

       input_document.close();
       FileOutputStream fileOut = null;
        FileOutputStream output_file =new FileOutputStream(new File("sdcard/projetos/enguelber/"+ nome + ".xls"));

        my_xls_workbook.write(output_file);

        output_file.close();

    }

If I remove the lines where I insert the image the application runs normally.

Libraries:

aa-poi-3.10-min-0.1.5

aa-poi-ooxml-schemas-3.10-reduced-more-0.1.5

commons-codec-1.7

poi-ooxml-schemas-3.10-reduced-less

ooxml-schemas-1.0

    
asked by anonymous 04.03.2016 / 00:57

2 answers

1

I was able to solve the problem by removing the libraries listed above and finally added the libraries below:

poi-3.11

commons-codec-1.9

No code needed to be made in the code. Thankful

    
05.03.2016 / 16:59
2

According to documentation the versions of your dependencies appear to be incompatible.

There it says the following:

  

ooxml-schemas-1.3.jar for POI 3.14, ooxml-schemas-1.0.jar for POI 3.5 and 3.6

It basically means that if you are using POI 3.10 it means that you should be using ooxml-schemas-1.1 and not 1.0 as in your list.

However, I noticed that you are using an custom version for Android and in the documentation says that aa-poi-ooxml-schemas-3.10-reduced-more-0.1.5.jar already contains ooxml-schemas embedded in the jar, then you would not need that jar. So the 1.0 version in your classpath is causing conflicts.

The simplest solution, it seems, would be to simply remove ooxml-schemas-1.0 and leave only the others.

In addition, poi-ooxml-schemas-3.10-reduced-less seems to be redundant. Please remove it too.

If none of this works, it seems that you are encountering the limitations of this version of the POI. The documentation says:

  

It may not work properly if the file contains Drawings or Charts. It may also fail if you try to write some styles.

This means it can fail if you add images or graphics as well as some types of styles.

You can take a test by removing the snippet that adds images from your code. If it works it is because unfortunately this version does not support these features well.

    
04.03.2016 / 02:42