【发布时间】:2017-03-09 14:55:52
【问题描述】:
在我的应用程序中,我使用 GD API 将文件上传到谷歌驱动器。它适用于小文件大小,但当文件大小很大(例如:200MB)时,它会抛出java.lang.OutOfMemoryError: 异常。我知道为什么它会崩溃,它将整个数据加载到内存中,有人可以建议我如何解决这个问题吗?
这是我的代码:
OutputStream outputStream = result.getDriveContents().getOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(file.getPath());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[8192];
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;
} catch (FileNotFoundException e) {
}
【问题讨论】:
-
毫无疑问需要使用更小的缓冲区,分块写入。
-
你能举个例子吗?
标签: java android google-drive-android-api