2016-06-21 308 views
-2

需要將圖像加載到byte []變量。BufferedReader to byte []

File file = new File(context.getFilesDir(), body + ".image"); 
BufferedReader in = new BufferedReader(new FileReader(file)); 

如何將BufferedReader轉換爲byte[]

回答

1

A Reader用於將字節轉換爲字符。這不是你想要的。您需要改爲InputStream。然後,您可以從流read()byte[]陣列可以根據需要,例如:

File file = new File(context.getFilesDir(), body + ".image"); 
InputStream in = new BufferedInputStream(new FileInputStream(file)); 
byte[] buf = new byte[file.length()]; 
int numRead = in.read(buf); 
+0

你從哪裏得到的字節needed'的'多少?可能從'in'中獲得? –

+1

@János如果你的目標是將整個文件加載到byte []中,你可以使用'file.length()'方法。 –