当进程仍在使用文件进行写入时,我尝试从文件创建字节数组块。实际上我正在将视频存储到文件中,并且我想在录制时从同一个文件创建块。
以下方法应该从文件中读取字节块:
private byte[] getBytesFromFile(File file) throws IOException{
InputStream is = new FileInputStream(file);
long length = file.length();
int numRead = 0;
byte[] bytes = new byte[(int)length - mReadOffset];
numRead = is.read(bytes, mReadOffset, bytes.length - mReadOffset);
if(numRead != (bytes.length - mReadOffset)){
throw new IOException("Could not completely read file " + file.getName());
}
mReadOffset += numRead;
is.close();
return bytes;
}
但问题是所有数组元素都设置为0,我猜这是因为写入过程锁定了文件。
如果你们中的任何人可以在写入文件时展示任何其他方式来创建文件块,我将不胜感激。