If I have a file that contains the exact byte[] data that I want:
[-119, 45, 67, ...]
How can I read those values into a byte array? Do I have to read them as a String, and then convert those String values to byte values?
This is what I'm currently doing, and it works, but slowly:
BufferedReader reader = null;
String mLine = null;
AssetManager assetManager = CustomApplication.getCustomAppContext().getAssets();
try {
reader = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
mLine = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String[] byteValues = mLine.substring(1, mLine.length() - 1).split(",");
byte[] imageBytes = new byte[byteValues.length];
for (int i = 0; i < imageBytes.length; i++) {
imageBytes[i] = Byte.valueOf(byteValues[i].trim());
}
return imageBytes;
UPDATE: There seems to be a misunderstanding of what I want to accomplish. My file contains the EXACT byte data I want in my final byte array. For example, I want to convert a file with exactly
[-119, 45, 67, ...]
into a byte[] called result with
result[0] = -119
result[1] = 45
result[2] = 67