我是嘗試將圖像保存到parse.com。我需要將其轉換爲字節數組。我決定嘗試這樣做的方式是使用apache commons-io。它工作不正常。這是我的代碼片段;java.io.FileNotFoundException:/file/path.jpg打開失敗:ENOENT(沒有這樣的文件或目錄)
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();
}
而我的錯誤是這樣的;
java.io.FileNotFoundException: /ClashMMA/res/drawable-hdpi/beatdown.jpg: open failed: ENOENT (No such file or directory)
但我相信,該文件是存在的,因爲我去了我的文件目錄(日食),右點擊,並點擊Copy Qualified Name
。這基本上覆制文件路徑。我嘗試了一些其他的路徑,比如我的電腦,以及我的src
文件夾。有人可以告訴我我做錯了什麼嗎?爲什麼當它實際上在那裏時不會讀取文件?請詳細說明。
我有這個問題。我刪除了以編程方式創建的文件夾,並手動創建並解決了問題! – breceivemail 2013-08-24 08:30:00