0

Writing an android app that uses excel files and ran into a bump in the road. It loads files without pictures just fine, but the moment I try to load an excel file that has pictures embedded in it, I get this errror:

    01-16 14:50:37.927 28377-28377/com.dcheeseman.csvflashcards W/System.err: org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:426)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:431)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:221)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at com.dcheeseman.csvflashcards.SpreadsheetReader.getPicturesXlsx(SpreadsheetReader.java:119)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at com.dcheeseman.csvflashcards.FlashCardActivity.onActivityResult(FlashCardActivity.java:215)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:6549)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4084)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4131)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at android.app.ActivityThread.access$1400(ActivityThread.java:179)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1511)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at android.os.Looper.loop(Looper.java:145)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5972)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err: Caused by: java.lang.reflect.InvocationTargetException
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at java.lang.reflect.Constructor.newInstance(Native Method)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:   ... 18 more
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err: Caused by: java.lang.ClassCastException: org.apache.xmlbeans.impl.values.XmlComplexContentImpl cannot be cast to org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing$Factory.parse(Unknown Source)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:     at org.apache.poi.xssf.usermodel.XSSFDrawing.<init>(XSSFDrawing.java:84)
01-16 14:50:37.947 28377-28377/com.dcheeseman.csvflashcards W/System.err:   ... 21 more

My concern is the class cast exception at the end. It's a pain getting POI to compile in an android environment and I had to use version 3.9 of POI. I'm wondering if this is a bug that just hasn't been fixed yet.

Here's the very short code segment related to it:

               String Fpath = data.getDataString();
                InputStream is2 = getContentResolver().openInputStream(Uri.parse(Fpath));
                List<XSSFPictureData> lst = SpreadsheetReader.getPicturesXlsx(is2);

public static List<XSSFPictureData> getPicturesXlsx(InputStream is)
    {
        List<XSSFPictureData> lst = null;
        XSSFWorkbook wb = null;
        try {
            wb = new XSSFWorkbook(is);
            lst = wb.getAllPictures();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lst;
    }

There error trips on the 'wb = new XSSFWorkbook(is);' line, which has worked just fine with excel files that have no images. Also of note, at this point in my code, no other application or code segment is accessing the file. Thanks in advance for any help.

Nuvious
  • 1,469
  • 2
  • 12
  • 21
  • 2
    The error is pretty clear. `org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing` is normally contained in `poi-ooxml-schemas-3.9...jar` and `org.apache.xmlbeans.impl.values.XmlComplexContentImpl` is normally contained in `xmlbeans-2.3.0.jar` with version 3.9. So I suspect your version of `xmlbeans-2.?.0.jar` will not suit. – Axel Richter Jan 17 '16 at 05:58
  • You nailed it. I also needed to use JarJar to fix follow-on issues but got it working finally. – Nuvious Jan 24 '16 at 21:32
  • @Nuvious Please show us correct answer which jar and version do you used. – Sushant Gosavi Dec 30 '19 at 17:52

0 Answers0