Java error inserting picture into spreadsheet. Apache Poi

Asked

Viewed 294 times

1

I am trying to insert an image into a sheet using Apache POI , but I am 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

  • The error says you are not finding a certain attribute in an Apache POI class. I did not investigate in detail, but if one POI class does not find an expected attribute in another from the same library, it could be the case that you are including dependencies from different versions of the same library. Check your dependencies, edit your question and put the version of each POI jar in your classpath.

  • 1

    @utluiz good evening thank you very much for the collaboration, as requested I added the information to the topic.

2 answers

2

According to information from documentation versions of its dependencies appear to be incompatible.

It says the following:

ooxml-schemas-1.3. jar for POI 3.14 or later, ooxml-schemas-1.1. jar for POI 3.7 up to POI 3.13, ooxml-schemas-1.0. jar for POI 3.5 and 3.6

Basically it means that if you are using POI 3.10 it means you should be using ooxml-schemas-1.1 and not the version 1.0 as on your list.

However, I noticed you’re wearing a customized version for Android and the documentation says that aa-poi-ooxml-schemas-3.10-reduced-more-0.1.5.jar already contains ooxml-schemas embedded in the jar, so you wouldn’t need this jar. So the version 1.0 in your classpath is causing conflicts.

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

In addition, the jar poi-ooxml-schemas-3.10-reduced-less seems to be redundant. Remove it too.

If none of this works, you seem to be finding the limitations of this version of POI. The documentation says:

It Might not work properly if the file contains Drawings or Charts. It also Might fail if you Try to write some Styles.

This means that it may fail to add images or graphics and also some types of styles.

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

  • I did the test but still with the error, I will redo the download of libraries but I will use the normal version without modifications.

  • @Matheusarrudalab Also removes the poi-ooxml-schemas-3.10-reduced-less, it should be redundant.

  • @Matheusarrudalab I edited the reply after reading the documentation more carefully.

  • did not give, I updated the library to version 3.11 with all the libraries compatible with the version and the error remains the same, if I remove the part of the image the code works perfectly, plus I’m generating reports and these photos will automatically pro report I’m thinking of using jxls along with apache poi, I make all the changes to Apache saved and open again with Jxls, tomorrow I’ll try and return here. Strong embrace, thank you for your attention.

  • Good idea to use Jxls and good luck. :)

1


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

poi-3.11

Commons-codec-1.9

No changes to the code were required. Grateful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.