安卓:编译错误Util.toByteArray(从一个例子取) [英] Android: compile error Util.toByteArray (taken from an example)

查看:594
本文介绍了安卓:编译错误Util.toByteArray(从一个例子取)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译code此示例在我的Andr​​oid应用程序有地穴/解密功能。我发现,code这里 http://apachejava.blogspot.it /2012/04/androidencryption-made-easy.html 我不知道,如果它是好的,但在这里,是不相关的。

I am trying to compile this sample of code in my Android app to have crypt/decrypt feature. I found that code here http://apachejava.blogspot.it/2012/04/androidencryption-made-easy.html I don't know if it's good but that's not relevant here.

在编译这一切都确定,但 Util.toByteArray 产生这个错误的Util解决不了。与utils的更换的Util不是任何有用的。

When compiling it's all OK but Util.toByteArray produces this error "Util cannot be resolved". Replacing Util with Utils is not any useful.

任何帮助吗?

推荐答案

所需的code部分缺失在你的页面链接:笔者忘记出示的Util 类这显然包含了toByteArray 函数

Part of the needed code is missing in the page you link : the author forgot to show his Util class which obviously contains a toByteArray function.

解决方法1:使用commons IO

替换

Util.toByteArray(cis);   

IOUtils.toByteArray(cis);

IOUtils是阿帕奇公地IO 的实用工具类。

您需要


  • 要下载公地IO罐子(见链接),并设置CLASSPATH相应

  • 此导入在你的类的启动:进口org.apache.commons.io.IOUtils;

  • to download the commons IO jar (see link) and set your classpath accordingly
  • this import at the start of your class : import org.apache.commons.io.IOUtils;

解决方案2:编写一个函数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(顺); toByteArray(顺);

这篇关于安卓:编译错误Util.toByteArray(从一个例子取)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆