The code is:
- Code: Select all
public void exportar(View view) {
WordprocessingMLPackage wordMLPackage = null;
try {
File notebookDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + NOTEBOOKS_DIR);
if (!notebookDir.exists()) {
notebookDir.mkdir();
}
wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.A4, false);
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Hello Word!");
File imageSmall = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "uruguay.gif");
File imageBig = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "foto.jpg");
// Our utility method wants that as a byte array
java.io.InputStream is = new java.io.FileInputStream(imageSmall );
long length = imageSmall.length();
// You cannot create an array using a long type.
// It needs to be an int type.
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
System.out.println("Could not completely read file "+imageSmall.getName());
}
is.close();
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
// Image 1: no width specified
org.docx4j.wml.P p = newImage( wordMLPackage, bytes,
filenameHint, altText,
id1, id2 );
wordMLPackage.getMainDocumentPart().addObject(p);
// Image 2: width 3000
org.docx4j.wml.P p2 = newImage( wordMLPackage, bytes,
filenameHint, altText,
id1, id2, 3000 );
wordMLPackage.getMainDocumentPart().addObject(p2);
// Image 3: width 6000
org.docx4j.wml.P p3 = newImage( wordMLPackage, bytes,
filenameHint, altText,
id1, id2, 6000 );
wordMLPackage.getMainDocumentPart().addObject(p3);
wordMLPackage.save(new File(notebookDir, "test" + DOCX_EXTENSION_FILE));
} catch (Docx4JException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4222)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4217)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lsun/awt/AppContext;
at ae.javax.imageio.spi.IIORegistry.getDefaultInstance(IIORegistry.java:155)
at ae.javax.imageio.ImageIO.<clinit>(ImageIO.java:65)
at org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderFactoryImageIO.getSupportedMIMETypes(ImageLoaderFactoryImageIO.java:39)
at org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderFactoryImageIO.isAvailable(ImageLoaderFactoryImageIO.java:54)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.registerLoaderFactory(ImageImplRegistry.java:186)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.discoverClasspathImplementations(ImageImplRegistry.java:115)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:79)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:88)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.getDefaultInstance(ImageImplRegistry.java:97)
at org.apache.xmlgraphics.image.loader.ImageManager.<init>(ImageManager.java:63)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.<clinit>(BinaryPartAbstractImage.java:126)
at uy.edu.doc4jtest.MainActivity.newImage(MainActivity.java:110)
at uy.edu.doc4jtest.MainActivity.exportar(MainActivity.java:74)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4217)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.ClassNotFoundException: Didn't find class "sun.awt.AppContext" on path: DexPathList[[zip file "/data/app/uy.edu.doc4jtest-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at ae.javax.imageio.spi.IIORegistry.getDefaultInstance(IIORegistry.java:155)
at ae.javax.imageio.ImageIO.<clinit>(ImageIO.java:65)
at org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderFactoryImageIO.getSupportedMIMETypes(ImageLoaderFactoryImageIO.java:39)
at org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderFactoryImageIO.isAvailable(ImageLoaderFactoryImageIO.java:54)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.registerLoaderFactory(ImageImplRegistry.java:186)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.discoverClasspathImplementations(ImageImplRegistry.java:115)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:79)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.<init>(ImageImplRegistry.java:88)
at org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.getDefaultInstance(ImageImplRegistry.java:97)
at org.apache.xmlgraphics.image.loader.ImageManager.<init>(ImageManager.java:63)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.<clinit>(BinaryPartAbstractImage.java:126)
at uy.edu.doc4jtest.MainActivity.newImage(MainActivity.java:110)
at uy.edu.doc4jtest.MainActivity.exportar(MainActivity.java:74)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4217)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Suppressed: java.lang.ClassNotFoundException: sun.awt.AppContext
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 27 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available