Outofmemoryory ByteArrayOutputStream将大文件发送到WS [英] Outofmemoryerror ByteArrayOutputStream sending large file to WS

查看:364
本文介绍了Outofmemoryory ByteArrayOutputStream将大文件发送到WS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将视频发送到Web服务,并且可以处理小于10MB的视频,如果该视频约为12MB,则出现内存不足错误

im sending videos to webservice and works ok with videos less than 10MB, if the video is about 12MB give me outofmemoryerror:

这是我的代码:

 FileInputStream fileInputStream = new FileInputStream(fichero);

                int bytesAvailable = fileInputStream.available();
                int maxBufferSize = 1024 * 1024 * 2;
                int bufferSize = Math.min(bytesAvailable, maxBufferSize);
                byte[] buffer = new byte[bufferSize];

                int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    // nuevos
                    byte byt[] = new byte[bufferSize];
                    fileInputStream.read(byt);

                    // nuevos
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                    // esto es nuevo
                    dos.write(buffer, 0, bufferSize);
                    // ya no es nuevo
                }

我认为这是因为即时消息正在缓冲所有视频,但是我不知道如何在不节省缓冲区的情况下发送此视频.

I think it is because im buffering all video, but i dont know how to send this without saviing in buffer.

这是堆栈错误:

08-31 08:54:20.925: E/AndroidRuntime(18476): Caused by: java.lang.OutOfMemoryError
08-31 08:54:20.925: E/AndroidRuntime(18476):    at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
08-31 08:54:20.925: E/AndroidRuntime(18476):    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:216)
08-31 08:54:20.925: E/AndroidRuntime(18476):    at org.apache.harmony.luni.internal.net.www.protocol.http.RetryableOutputStream.write(RetryableOutputStream.java:60)
08-31 08:54:20.925: E/AndroidRuntime(18476):    at java.io.DataOutputStream.write(DataOutputStream.java:99)
08-31 08:54:20.925: E/AndroidRuntime(18476):    at com.reparalia.movilidad.AdjuntarFicheros$SubeFichero.doInBackground(AdjuntarFicheros.java:702)
08-31 08:54:20.925: E/AndroidRuntime(18476):    at com.reparalia.movilidad.AdjuntarFicheros$SubeFichero.doInBackground(AdjuntarFicheros.java:1)

702行是dos.write(buffer,0,bufferSize);

The 702 line is dos.write(buffer, 0, bufferSize);

有什么方法可以发送视频吗?谢谢

There are any way to send the video?Thanks

推荐答案

ByteArrayOutputStream通过默认情况下分配32字节或构造函数指定的内存量开始.当缓冲区已满时,ByteArrayOutputStream 加倍缓冲区的大小.对于大型物体,这可能是一个真正的问题.最好的选择之一就是

ByteArrayOutputStream starts out by allocating either 32 bytes or a constructor-specified amount of memory by default. When that buffer has been filled-up, ByteArrayOutputStream doubles the size of the buffer. For large objects, this can be a real problem. Your best alternative is to either

  1. 使用构造函数指定的缓冲区大小,或者
  2. 扩展ByteArrayOutputStream并覆盖write方法,以便重新分配对您的流更有利.
  1. Make use of the constructor-specified buffer size, or
  2. Extend ByteArrayOutputStream and override the write methods so that reallocation is more advantageous for your stream.

这篇关于Outofmemoryory ByteArrayOutputStream将大文件发送到WS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆