gpt4 book ai didi

android - java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java :91)? 处的 java.lang.OutOfMemoryError

转载 作者:行者123 更新时间:2023-11-29 19:12:11 26 4
gpt4 key购买 nike

我在将文件上传到google drive时遇到这个问题,我正在将录制的音频上传到google drive,当时出现了这个异常

用于写入文件内容的代码

                      OutputStream outputStream = result.getDriveContents().getOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
byte[] photoBytes = baos.toByteArray();
outputStream.write(photoBytes);

outputStream.close();
outputStream = null;
fis.close();
fis = null;

Log.e("File Size", "Size " + file.length());

} catch (FileNotFoundException e) {
Log.v("EXCEPTION", "FileNotFoundException: " + e.getMessage());
} catch (IOException e1) {
Log.v("EXCEPTION", "Unable to write file contents." + e1.getMessage());
}

异常发生在`baos.write(buf, 0, n);这行

请帮我解决这个错误。`

最佳答案

首先写入 ByteArrayOutputStream 意味着完整的文件将最终进入 JVM 堆。根据文件大小和堆大小,这可能是不可能的,因此异常(exception)。如果您不需要 ByteArrayOutputStream 做任何其他事情,只需直接写入 outputStream:

OutputStream outputStream = result.getDriveContents().getOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(file);
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
outputStream.write(buf, 0, n);


} catch (FileNotFoundException e) {
Log.v("EXCEPTION", "FileNotFoundException: " + e.getMessage());
} catch (IOException e1) {
Log.v("EXCEPTION", "Unable to write file contents." + e1.getMessage());
} finally {
outputStream.close();
fis.close();
Log.e("File Size", "Size " + file.length());
}

P.S.:如果引用很快超出范围,则没有必要清空引用...

关于android - java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java :91)? 处的 java.lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44963653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com