我想在上传之前在Android中压缩视频。我正在关注Stack的代码,当我尝试它时,我可以看到该文件已被压缩(不确定它是否已实际压缩)但它减少了文件大小并按预期创建了压缩文件但我无法打开文件,因为它的内容是乱码,所以我尝试解压缩视频,因为我可以肯定知道它已成功压缩,反过来导致解压缩。 我的问题是原始文件大小和解压缩文件大小是 SAME ,但文件没有打开,它说"抱歉,该视频无法播放"。
代码:
压缩:
public static void compressData(byte[] data) throws Exception {
OutputStream out = new FileOutputStream(new
File("/storage/emulated/0/DCIM/Camera/compressed_video.mp4"));
Log.e("Original byte length: ", String.valueOf(data.length));
Deflater d = new Deflater();
DeflaterOutputStream dout = new DeflaterOutputStream(out, d);
dout.write(data);
dout.close();
Log.i("The Compressed Byte array is ", ""+data.length);
Log.e("Compressed byte length: ",
String.valueOf(dout.toString().getBytes().length));
}
减压:
public static void decompress() throws Exception {
InputStream in = new FileInputStream("/storage/emulated/0/DCIM/Camera/compressed_video.mp4");
InflaterInputStream ini = new InflaterInputStream(in);
ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
int b;
while ((b = ini.read()) != -1) {
bout.write(b);
}
ini.close();
bout.close();
String s = new String(bout.toByteArray());
System.out.println(s);
File decompressed_file = new File("/storage/emulated/0/DCIM/Camera/decompressed_video.mp4");
FileOutputStream out_file = new FileOutputStream(decompressed_file);
out_file.write(bout.toByteArray());
out_file.close();
Log.i("The Decompressed Byte array is ", ""+bout.toByteArray().length);
Log.e("De-compressed byte length: ",
String.valueOf(bout.toByteArray().length));
}
从上面的代码中,原始字节长度和解压缩的字节长度是相同的,但我不确定为什么字节数组不会写入文件。我可以看到compress_video和decompressed_video这两个文件已创建,但我也无法播放。无法播放compressed_video.mp4是可以接受的,但我应该可以播放无法播放的decompressed_video.mp4。我已经坐了2天以上,所以任何帮助都会非常感激。先谢谢你们。