【问题标题】:NoClassDefFoundError when inserting image in Android app using Apache poi & MS Excel使用 Apache poi 和 MS Excel 在 Android 应用程序中插入图像时出现 NoClassDefFoundError
【发布时间】:2014-02-22 11:16:23
【问题描述】:

我想使用 Apache poi 将 PNG 图像插入到我的 Excel 工作表中。

为此,我使用以下代码:

//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();

但第一次我遇到了一个错误,我通过添加这个库commons-codec-1.8.jar 解决了这个错误,现在我遇到了这个错误:

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)

哪个指向这条线:

pict.resize();

我该如何解决?

【问题讨论】:

  • 你找到解决这个问题的方法了吗?

标签: android excel image apache-poi


【解决方案1】:

您不能使用涉及图形例程 (java.awt) 的纯 Java 库,因为 Android 尚未实现它们,因为它使用自己的图形库。底线是,你不能在 Android 上使用你想要的库,尽管我确信还有其他方法可以完成你想做的任务。

Android 使用了一些基本类型,即Views、CanvasDrawables。看看这些,看看它们是否满足您的需求。

【讨论】:

  • 我知道这些类型并且已经使用过它们,但是现在我正在使用需要这些类的apache poi库,不是我需要它们,你明白吗?
  • 哦,我希望没有,因此,无法通过 apache poi 在 android 中插入图像
  • 你不能在Android中使用任何标准的Java图形库,也不能使用任何扩展图形库的东西。 Android 只是没有实现这一点,因为他们使用自己的库。
  • 完全同意@PearsonArtPhoto
  • 四处寻找一个专用的 Android 库。 github.com/dennis-sheil/android-spreadsheet 看起来可能有效,但可能还有其他方法。
【解决方案2】:

不能在 Android 中使用java.awt.* 包,请改用Android UI elements。见related question here

【讨论】:

  • 我已经导入了这个但是同样的问题
  • 我想你误会了:你不能导入这个!此包中的类的用法在 Android 中支持
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多