centic9 / poi-on-android

A sample project that shows how Apache POI can be used in an Android application
Apache License 2.0
357 stars 89 forks source link

Release build throws a OpenXML4JRuntimeException: Fail to save: error #74

Closed Stormtroeperr closed 4 years ago

Stormtroeperr commented 4 years ago

Good day! Whenever I run my android application on release mode it crashes on XWPFDocument.write(out) with the following error.

org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package: The part /docProps/app.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@29b8687

I tried to google the error but I couldn't find a solution that worked for me.

public class DocumentParser extends Thread { private XWPFDocument _document;

private DocumentInformation _documentInformation;
private Context _context;
private Uri _documentPath;

private ArrayList<ILoadingListener> listeners;

public DocumentParser(DocumentInformation information, Context context, Uri documentPath, ILoadingListener listener){

    System.setProperty("org.apache.poi.javax.xml.stream.XMLInputFactory", "com.fasterxml.aalto.stax.InputFactoryImpl");
    System.setProperty("org.apache.poi.javax.xml.stream.XMLOutputFactory", "com.fasterxml.aalto.stax.OutputFactoryImpl");
    System.setProperty("org.apache.poi.javax.xml.stream.XMLEventFactory", "com.fasterxml.aalto.stax.EventFactoryImpl");

    listeners = new ArrayList<>();
    listeners.add(listener);

    _document = new XWPFDocument();
    _documentInformation = information;
    _context = context;
    _documentPath = documentPath;
}

private void create(Context context){
    try{
        ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(_documentPath, "w");
        FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
        createTitlePage(context);

        _document.write(out); // WHERE THE APP PROBABLY CRASHES
        out.close();

        mailDocuments(context);
    } catch (Exception e){
        Log.e("outputstream", e.toString());
        updateLoadingMessage(e.toString());
    }
}

Thanks for the help in advance!

centic9 commented 4 years ago

There should be more log-output if you enable logging with -Dorg.apache.poi.util.POILogger=org.apache.poi.util.SystemOutLogger or -Dorg.apache.poi.util.POILogger=org.apache.poi.util.CommonsLogger which might explain what happens?

Possible reasons can be missing rights to write the file at that location.

Stormtroeperr commented 4 years ago

Hey, Centic thanks for coming back to me so soon!

I did as you said and for some magical reason, it all started working before I could even try the logger on the release build.

I absolutely have no clue why but ill take it :)

Thanks for the help!