gpt4 book ai didi

Android:上传视频时出现 OutOfMemoryError - 如何最好地分块?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:32 24 4
gpt4 key购买 nike

我有同样的问题 as described here ,但我会提供更多细节。尝试在 Android 中上传视频时,我正在将其读入内存,如果视频很大,我会收到 OutOfMemoryError。

这是我的代码:

// get bytestream to upload
videoByteArray = getBytesFromFile(cR, fileUriString);

public static byte[] getBytesFromFile(ContentResolver cR, String fileUriString) throws IOException {
Uri tempuri = Uri.parse(fileUriString);
InputStream is = cR.openInputStream(tempuri);
byte[] b3 = readBytes(is);
is.close();
return b3;
}
public static byte[] readBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
// this is storage overwritten on each iteration with bytes
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}

这是回溯(错误在 byteBuffer.write(buffer, 0, len) 行上抛出):

04-08 11:56:20.456: ERROR/dalvikvm-heap(6088): Out of memory on a 16775184-byte allocation.
04-08 11:56:20.456: INFO/dalvikvm(6088): "IntentService[UploadService]" prio=5 tid=17 RUNNABLE
04-08 11:56:20.456: INFO/dalvikvm(6088): | group="main" sCount=0 dsCount=0 s=N obj=0x449a3cf0 self=0x38d410
04-08 11:56:20.456: INFO/dalvikvm(6088): | sysTid=6119 nice=0 sched=0/0 cgrp=default handle=4010416
04-08 11:56:20.456: INFO/dalvikvm(6088): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~93)
04-08 11:56:20.456: INFO/dalvikvm(6088): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
04-08 11:56:20.456: INFO/dalvikvm(6088): at com.android.election2010.UploadService.readBytes(UploadService.java:199)
04-08 11:56:20.456: INFO/dalvikvm(6088): at com.android.election2010.UploadService.getBytesFromFile(UploadService.java:182)
04-08 11:56:20.456: INFO/dalvikvm(6088): at com.android.election2010.UploadService.doUploadinBackground(UploadService.java:118)
04-08 11:56:20.456: INFO/dalvikvm(6088): at com.android.election2010.UploadService.onHandleIntent(UploadService.java:85)
04-08 11:56:20.456: INFO/dalvikvm(6088): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:30)
04-08 11:56:20.456: INFO/dalvikvm(6088): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 11:56:20.456: INFO/dalvikvm(6088): at android.os.Looper.loop(Looper.java:123)
04-08 11:56:20.456: INFO/dalvikvm(6088): at android.os.HandlerThread.run(HandlerThread.java:60)
04-08 11:56:20.467: WARN/dalvikvm(6088): threadid=17: thread exiting with uncaught exception (group=0x4001b180)
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): Uncaught handler: thread IntentService[UploadService] exiting due to uncaught exception
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): java.lang.OutOfMemoryError
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:93)
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): at com.android.election2010.UploadService.readBytes(UploadService.java:199)
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): at com.android.election2010.UploadService.getBytesFromFile(UploadService.java:182)
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): at com.android.election2010.UploadService.doUploadinBackground(UploadService.java:118)
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): at com.android.election2010.UploadService.onHandleIntent(UploadService.java:85)
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:30)
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): at android.os.Looper.loop(Looper.java:123)
04-08 11:56:20.467: ERROR/AndroidRuntime(6088): at android.os.HandlerThread.run(HandlerThread.java:60)
04-08 11:56:20.496: INFO/Process(4657): Sending signal. PID: 6088 SIG: 3

我想 as @DroidIn suggests ,我需要分块上传。但是(新手问题警报)这是否意味着我应该发出多个 PostMethod 请求,并在服务器端将文件粘合在一起?或者我可以将字节流分块加载到内存中,然后在 Android 代码中将它们粘合在一起吗?

如果有人能给我提供有关最佳方法的线索,我将不胜感激。

最佳答案

Jim Blackler 是绝对正确的。你不能在 byte[] 的内存中有完整的视频。所以你应该读取文件而不是 byte[] 而是直接到 HttpUrlConnectio.getOutputStream()。

但即使在那之后你仍然会得到 OutOfMemory,因为 HttpUrlConnection 将所有输出数据缓存在内存中并仅在你完成写入时将其发送到网络。

要覆盖此行为,您可以使用 HttpUrlConnection.setChunkedStreamingMode() 或 HttpUrlConnection.setFixedLengthStreamingMode()。如果您的服务器接受流 block ,您可以使用 setChunkedStreamingMode()。否则,您将不得不使用 setFixedLengthStreamingMode。要使用 setFixedLengthStreamingMode,您需要在开始流式传输数据之前知道确切的内容长度,因此这有点棘手。

此时不再有 OutOfMemory。

如果您有视频文件,您可以选择另一种方法,更简单。像那样使用 HttpClient 和 FileBody How to send HTTP POST request and receive response? .我认为他们实现了上述所有方法,您不必为此担心。

关于Android:上传视频时出现 OutOfMemoryError - 如何最好地分块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2599305/

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