小编Ash*_*dya的帖子

将3GB文件转换为字节数组

我试图将3GB文件转换为字节数组但获取OOM(OutOfMemoryError).

我们尝试了

RandomAccessFile randomAccessFile = new RandomAccessFile(sourceLocation.getAbsolutePath(), "r");
MappedByteBuffer mappedByteBuffer = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length()); //while it is large file, it threw 'mmap failed: ENOMEM (Out of memory)' exception.

byte[] data = new byte[1024 * 1024];  // 1MB read at time
while (mappedByteBuffer.hasRemaining()) {
    int remaining = data.length;

    if (mappedByteBuffer.remaining() < remaining)
        remaining = mappedByteBuffer.remaining();
        mappedByteBuffer.get(data, 0, remaining);
    }

    mappedByteBuffer.rewind();
    byte fileContent[] = mappedByteBuffer.array(); //while it is small file, it threw 'java.nio.ReadOnlyBufferException' exception.
    randomAccessFile.close();
}
Run Code Online (Sandbox Code Playgroud)

我的自定义请求正文:我的请求正文准备的自定义请求正文类

import android.os.Looper; …
Run Code Online (Sandbox Code Playgroud)

java android out-of-memory

2
推荐指数
1
解决办法
193
查看次数

标签 统计

android ×1

java ×1

out-of-memory ×1