Android中200MB+大视频转Base64遇OOM问题求助
Android大视频文件转Base64避免OOM的解决方案
核心原则:分块处理,避免一次性加载全量数据
200MB以上的视频文件直接加载到内存编码会瞬间耗尽应用内存,引发OOM。解决的核心是分块读取文件内容,逐块编码Base64,同时避免在内存中存储完整的Base64结果。
具体实现步骤
1. 选择合适的分块大小
分块大小建议设置在8KB-32KB之间(可根据设备内存情况调整):
- 过大的块会占用过多内存,仍有OOM风险
- 过小的块会增加IO操作次数,降低编码效率
2. 分块读取+逐块编码
使用FileInputStream分块读取视频文件,每读取一块就进行Base64编码,然后直接写入输出文件或上传流,不保留完整的Base64字符串在内存中:
private static final int BLOCK_SIZE = 16 * 1024; // 16KB分块 public void encodeVideoToBase64(File sourceVideo, File targetBase64File) throws IOException { try (FileInputStream inputStream = new FileInputStream(sourceVideo); FileOutputStream outputStream = new FileOutputStream(targetBase64File)) { byte[] buffer = new byte[BLOCK_SIZE]; int bytesRead; Base64.Encoder encoder = Base64.getEncoder().withoutPadding(); // 禁用填充,减少冗余字符 while ((bytesRead = inputStream.read(buffer)) != -1) { // 仅编码实际读取的字节,避免处理无效空字节 byte[] encodedChunk = encoder.encode(buffer, 0, bytesRead); outputStream.write(encodedChunk); // 可选:手动触发GC清理临时内存(仅在内存压力大时使用,避免频繁调用) System.gc(); } } }
3. 边编码边上传(进阶优化)
如果服务器支持流式上传,无需生成完整的Base64文件,可直接将编码后的字节块写入上传流,进一步节省磁盘和内存空间:
public void uploadVideoAsBase64(File sourceVideo, HttpURLConnection connection) throws IOException { try (FileInputStream inputStream = new FileInputStream(sourceVideo); OutputStream uploadStream = connection.getOutputStream()) { byte[] buffer = new byte[16 * 1024]; int bytesRead; Base64.Encoder encoder = Base64.getEncoder().withoutPadding(); while ((bytesRead = inputStream.read(buffer)) != -1) { byte[] encodedChunk = encoder.encode(buffer, 0, bytesRead); uploadStream.write(encodedChunk); uploadStream.flush(); // 及时刷新流,避免内存堆积 } } }
关键优化细节
- 关闭资源自动管理:使用try-with-resources语法,确保输入输出流自动关闭,避免内存泄漏
- 禁用Base64填充:使用
withoutPadding()减少编码后的冗余字符,降低输出体积 - 避免内存存储完整结果:绝对不要将整个Base64字符串存储在
String或ByteArray中,必须通过流写入文件或直接上传 - 辅助内存优化:在AndroidManifest.xml中为应用开启大内存堆(仅作为辅助手段,不能依赖):
<application android:largeHeap="true" ...> </application>
禁止的错误做法
- 不要使用
File.readAllBytes()一次性读取整个视频文件到内存 - 不要将完整的Base64结果存储在内存中(如拼接成
String) - 不要使用过小或过大的分块,平衡内存占用与IO效率
内容的提问来源于stack exchange,提问作者Silence
相关产品推荐
相关产品推荐

