gpt4 book ai didi

Android HttpsURLConnection on 2.3.4 OutOfMemoryError while uploading Large File (byte[] in memory)

转载 作者:行者123 更新时间:2023-11-30 03:40:37 25 4
gpt4 key购买 nike

我试图在服务器上上传一个大约 10 MB 的大文件,并意识到在 2.3.4 上,流先写入内存,然后再写入服务器,我通过查看堆内存转储确认了这种行为,因为这个对于大文件,它会导致 OutOfMemory 异常。我在 4.2 设备上看不到相同的行为。

以下是我使用的代码:

    URL url = new URL(uri);
connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("content-type", "");
connection.setRequestProperty("Accept-Encoding", "");
connection.setFixedLengthStreamingMode((int)totalBytes);
out = new BufferedOutputStream(connection.getOutputStream());
fis = new BufferedInputStream(new FileInputStream(file));

byte[] buffer = new byte[32 * 1024];
int bytesRead = 0;
int totalBytesRead = 0;
while ((bytesRead = fis.read(buffer)) != -1)
{
totalBytesRead = totalBytesRead + bytesRead;
out.write(buffer, 0, bytesRead);// OOM Error
}

out.flush();

最佳答案

我向 google-android 提交了一个错误,他们声称它已为 GingerBread 版本修复,但我仍然能够在 2.3.4 中复制该问题

错误链接 https://code.google.com/p/android/issues/detail?id=53946https://code.google.com/p/android/issues/detail?id=3164

我最终为 EclairFroyoGingerBread 使用了 HttpClient和 HttpURLConnection 用于 HoneyComb 及以上

关于Android HttpsURLConnection on 2.3.4 OutOfMemoryError while uploading Large File (byte[] in memory),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15798824/

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