【发布时间】:2020-04-16 05:55:49
【问题描述】:
我正在尝试使用以下代码编码视频文件。
byte[] decodedBytes = Base64.decode(video, Base64.DEFAULT);
InputStream input = new ByteArrayInputStream(decodedBytes);
OutputStream output = null;
try {
File file = new File (Environment.getExternalStorageDirectory().toString(),"video.mp4");
output = new FileOutputStream(file);
byte[] data = new byte[decodedBytes.length];
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
output.close();
Uri video = Uri.parse(file.getAbsolutePath());
videoView.setVideoURI(video);
videoView.start();
} catch (IOException e) {
Log.e("check123", e.toString());
e.printStackTrace();
}
但我在运行时收到此 错误 消息:
应用程序无法播放此视频。
【问题讨论】:
-
byte[] data = new byte[decodedBytes.length]; int count; while ((count = input.read(data)) != -1) { output.write(data, 0, count); }。替换为output.write(decodedBytes);。 -
你能建议我另一种方法来解码编码的base64视频字符串并将其设置在android studio的videoview上吗?
-
这是我正在使用的编码代码:videoView.buildDrawingCache();位图位图 = videoView.getDrawingCache(); java.io.ByteArrayOutputStream 流=新 java.io.ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.WEBP, 90, 流); byte[] videoByteArray=stream.toByteArray(); video = Base64.encodeToString(videoByteArray, 0);
-
另一种方式?你试过我的代码吗?为什么不举报?
-
是的,我试过你的代码。
标签: java android file file-handling