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.
When compiling it's all OK but Util.toByteArray produces this error "Util cannot be resolved". Replacing Util with Utils is not any useful.
Any help?
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.
Solution 1 : use commons IO
Replace
Util.toByteArray(cis);
by
IOUtils.toByteArray(cis);
IOUtils is a Apache commons IO utility class.
You'll need
- 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;
Solution 2 : write a toByteArray function
Define this function :
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();
}
And replace Util.toByteArray(cis); by toByteArray(cis);.
来源:https://stackoverflow.com/questions/11540018/android-compile-error-util-tobytearray-taken-from-an-example