I am trying to save an image to parse.com. I need to convert it to byte array. The way I have decided to try to do this is to use apache commons-io. It is not quite working properly. This is my code snippet;
private void saveImage() throws IOException {
// TODO Auto-generated method stub
InputStream header = new FileInputStream("/ClashMMA/res/drawable-hdpi/beatdown.jpg");
ParseFile file = new ParseFile(toByteArray(header));
try{
file.save();
} catch (ParseException e) {
e.printStackTrace();
}
ParseObject displayImage = new ParseObject("displayImage");
displayImage.put("header", file);
try{
displayImage.save();
} catch (ParseException e1){
e1.printStackTrace();
}
}
private byte[] toByteArray(InputStream header) throws IOException {
// TODO Auto-generated method stub
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int l;
byte[] data = new byte[1024];
while ((l = header.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, l);
}
buffer.flush();
return buffer.toByteArray();
}
And my error is this;
java.io.FileNotFoundException: /ClashMMA/res/drawable-hdpi/beatdown.jpg: open failed: ENOENT (No such file or directory)
But I am sure that the file is there, because I went to my file directory (in eclipse), right clicked, and clicked on Copy Qualified Name
. Which essentially copies the file path. I have tried a few other paths, Like right off of my computer, and in my src
folder. Can someone please tell me what I am doing wrong? Why won't it read the file when in fact it is there? Detailed explanations please.
The files in your Eclipse projects are not in the (real or emulated) Android file system, and that is where the Android FileInputStream
is looking. (With good reason! The host file system for Eclipse won't be there on a real Android device.)
You've basically got two choices:
get them into the APK archive so that you can load them using getSystemResourceAsStream
: see How to load image for Android ImageView from classpath?
copy the files into the Android file system so that FileInputStream
can find them.