我想在 base64 字符串中转换视频,所以我通过 android 中的视频转换 migBase64 方法,它将视频成功转换为字符串,但是当我将字符串解码为视频时,它无法在视频中正确转换。所以如果有人知道请帮助我。
我尝试如下代码:
String encodedString;
//Decode Video To String
File tempFile = new File(Environment.getExternalStorageDirectory()+ "/my/part/my_0.mp4");
byte fileContent[] = new byte[3000];
try {
FileInputStream fin = new FileInputStream(tempFile);
while (fin.read(fileContent) >= 0) {
// b.append(Base64.encodeToString(fileContent, true));
encodedString = Base64.encodeToString(fileContent, true);
}
} catch (IOException e) {
}
//Encoding Video To String Successfully.
//Decode String To Video
try {
byte[] decodedBytes = Base64.decodeF
File file2 = new File(Environment.getExternalStorageDirectory()
+ "/my/Converted.mp4");
FileOutputStream os = new FileOutputStream(file2, true);
os.write(decodedBytes);
os.close();
} catch (Exception e) {
// TODO: handle exception
Log.e("Error", e.toString());
}
// Problem is in Decoding.
我的问题是将字符串解码为视频,我的原始视频为 1 MB,解码视频后为 1.1 kb,它无法转换我的原始视频,请帮助我。
我解决了我的问题,我发布代码以寻求帮助。
//Encode Video To String With mig Base64.
File tempFile = new File(Environment.getExternalStorageDirectory()
+ "/my/part/my_0.mp4");
String encodedString = null;
InputStream inputStream = null;
try {
inputStream = new FileInputStream(tempFile);
} catch (Exception e) {
// TODO: handle exception
}
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();
encodedString = Base64.encodeToString(bytes, true);
Log.i("Strng", encodedString);
//Decode String To Video With mig Base64.
byte[] decodedBytes = Base64.decodeFast(encodedString.getBytes());
try {
FileOutputStream out = new FileOutputStream(
Environment.getExternalStorageDirectory()
+ "/my/Convert.mp4");
out.write(decodedBytes);
out.close();
} catch (Exception e) {
// TODO: handle exception
Log.e("Error", e.toString());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)