繁体   English   中英

如何将视频文件转换为字节数组?

[英]How to convert the Video file to byte array?

我需要将视频文件转换为字节数组,然后将字节数组发送到服务器,但是当我上传字节数组时,服务器没有以正确的格式接收它。 我转换文件如下: -

buffer=new byte[1024];
os=new ByteArrayOutputStream();
FileInputStream fis=new FileInputStream(file);
int read;
while ((read=fis.read(buffer))!=-1){
    os.write(buffer,0,read);
}
fis.close();
os.close();

以下是将文件转换为byte[]的方法

第一的

File file = new File(path);
int size = (int) file.length();
byte[] bytes = new byte[size];
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();

第二

byte bytes[] = new byte[(int) file.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
DataInputStream dis = new DataInputStream(bis);
dis.readFully(bytes);

第三

byte bytes[] = FileUtils.readFileToByteArray(photoFile)

唯一的缺点是在 build.gradle 应用程序中添加此依赖项:

implementation 'commons-io:commons-io:2.5'
File file = new File("filepath");
//init array with file length
byte[] bytesArray = new byte[(int) file.length()]; 

FileInputStream fis = new FileInputStream(file);
fis.read(bytesArray); //read file into bytes[]
fis.close();
return bytesArray;

尝试这个! 希望它会起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM