【问题标题】:Android: compile error Util.toByteArray (taken from an example)Android:编译错误 Util.toByteArray(取自示例)
【发布时间】:2012-07-17 09:48:20
【问题描述】:

我正在尝试在我的 Android 应用程序中编译此代码示例以具有加密/解密功能。我在这里找到了代码http://apachejava.blogspot.it/2012/04/androidencryption-made-easy.html 我不知道它是否好,但这与这里无关。

编译时一切正常,但Util.toByteArray 产生此错误“Util 无法解析”。用 Utils 代替 Util 没有任何用处。

有什么帮助吗?

【问题讨论】:

  • 链接(代码的来源)多年来似乎已经断开。

标签: java android eclipse compilation


【解决方案1】:

您链接的页面中缺少部分所需代码:作者忘记显示他的 Util 类,该类显然包含一个 toByteArray function

解决方案 1:使用 commons IO

替换

Util.toByteArray(cis);   

通过

IOUtils.toByteArray(cis);

IOUtils 是一个 Apache commons IO 实用程序类。

你需要

  • 下载 commons IO jar(参见链接)并相应地设置您的类路径
  • 在课程开始时导入:import org.apache.commons.io.IOUtils;

方案二:写一个toByteArray函数

定义这个函数:

public byte[] toByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int l;
    byte[] data = new byte[1024];
    while ((l = is.read(data, 0, data.length)) != -1) {
      buffer.write(data, 0, l);
    }
    buffer.flush();
    return buffer.toByteArray();
}

并将Util.toByteArray(cis); 替换为toByteArray(cis);

【讨论】:

  • thx @dystroy 但我得到“导入......无法解决”。如何将其包含在项目中?
  • 查看编辑后的答案。选择您喜欢的解决方案。如果您不需要其他 Apache 实用程序,第二个当然会更容易。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多