NoClassDefFoundError when inserting image in Android app using Apache poi & MS Excel

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

I want to insert a PNG image into my Excel sheet using Apache poi.

To do that I use this code:

//add picture data to this workbook. InputStream is = new FileInputStream("/sdcard/MYAPPFOLDER/logo_app.png"); byte[] bytes = IOUtils.toByteArray(is); int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); is.close();  CreationHelper helper = workbook.getCreationHelper();  // Create the drawing patriarch.  This is the top level container for all shapes.  Drawing drawing = sheet.createDrawingPatriarch();  //add a picture shape ClientAnchor anchor = helper.createClientAnchor(); //set top-left corner of the picture, //subsequent call of Picture#resize() will operate relative to it anchor.setCol1(0); anchor.setRow1(0); Picture pict = drawing.createPicture(anchor, pictureIdx);  //auto-size picture relative to its top-left corner pict.resize();

But in first time I have one error which I have solved by adding this library commons-codec-1.8.jar and now I have this error:

02-21 10:10:51.466: E/AndroidRuntime(31691): FATAL EXCEPTION: main 02-21 10:10:51.466: E/AndroidRuntime(31691): java.lang.NoClassDefFoundError: java.awt.Dimension 02-21 10:10:51.466: E/AndroidRuntime(31691):    at org.apache.poi.ss.util.ImageUtils.getImageDimension(ImageUtils.java:52) 02-21 10:10:51.466: E/AndroidRuntime(31691):    at org.apache.poi.hssf.usermodel.HSSFPicture.getImageDimension(HSSFPicture.java:243) 02-21 10:10:51.466: E/AndroidRuntime(31691):    at org.apache.poi.hssf.usermodel.HSSFPicture.getPreferredSize(HSSFPicture.java:163)

Which points to this line:

pict.resize();

How can I fix it?

回答1:

You can't use pure Java libraries that involve graphics routines (java.awt), because Android hasn't implemented them, as it uses it's own graphics library. Bottom line is, you can't use the library you want with Android, although I'm sure there's other ways to do the task you want to do.

Android uses a few base types, namely Views, Canvas, and Drawables. Take a look at those, and see if they meet your needs.



回答2:

you cannot use java.awt.* package in Android, use the Android UI elements instead. See related question here



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!