java - 上传文件超过300M时应用程序崩溃

标签 java android

当我选择上传 400M 文件时,我的应用程序崩溃了,小于 300M 上传正常

我改变了这个

int maxBufferSize =  10 * 10241 * 1024;

int maxBufferSize =   10241 * 1024;

还是同样的问题

我是不是错过了什么?

protected String doInBackground(String... urls) {

    String upLoadServerUri = upApi.uploadUrl;
    String fileName = this.file_path;
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize =  10 * 10241 * 1024;
    File sourceFile = new File(fileName);
    int sentBytes = 0;
    long fileSize = sourceFile.length();

    try
    {
        FileInputStream fileInputStream = new FileInputStream(new File(fileName));

        URL url = new URL(upLoadServerUri);
        connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setChunkedStreamingMode(1024);
        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type",     "multipart/form-data;boundary="+boundary);

        outputStream = new DataOutputStream(     connection.getOutputStream() );
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"file[]\";filename=\""+ fileName + "\"" + lineEnd);
        outputStream.writeBytes(lineEnd);

        buffer = new byte[maxBufferSize];

        while (true)
        {
            int bytesToRead = Math.min(maxBufferSize, fileInputStream.available());

            // Break if complete
            if(bytesToRead == 0){ break; }

            // Read bytes
            bytesRead = fileInputStream.read(buffer, 0, bytesToRead);

            // Write bytes
            outputStream.write(buffer, 0, bytesRead);

            // Update progress dialog
            sentBytes += bytesRead;

            // Publish progress
            double progress = (double)sentBytes / fileSize;
            publishProgress((int)progress);
        }
<小时/>
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): ION: alloc_data: handle(0xE4904BC0), len(368640), align(8192), flags(0x2000100), fd_data: handle(0xe4904bc0), fd(0x46)
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): Allocate done for all i/p buffers
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): Allocate done for all o/p buffers
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): OMX_CommandStateSet complete, m_state = 2
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): send_command: Recieved a Command from Client
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): send_command_proxy(): cmd = 0, Current State 2, Expected State 3
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): send_command_proxy(): OMX_CommandStateSet issued
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): Current State 2, Expected State 3
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): send_command_proxy(): Idle-->Executing
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): send_command: Command Processed
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): Rxd OMX_COMPONENT_GENERATE_START_DONE
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): Rxd i/p EOS, Notify Driver that EOS has been reached
04-04 23:11:24.694: E/OMX-VDEC-1080P(217): Output EOS has been reached
04-04 23:11:24.694: E/OMX-VDEC-1080P(217): Rxd OMX_COMPONENT_GENERATE_EOS_DONE
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command: Recieved a Command from Client
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): cmd = 0, Current State 3, Expected State 2
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): OMX_CommandStateSet issued
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Current State 3, Expected State 2
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Command Recieved in OMX_StateExecuting
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): Executing --> Idle
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Driver flush i/p Port complete
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Initiate Input Flush
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Reset all the variables before flusing
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Initialize parser
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): OMX flush i/p Port complete PenBuf(0)
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Driver flush o/p Port complete
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Initiate Output Flush
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): OMX flush o/p Port complete PenBuf(0)
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Rxd OMX_COMPONENT_GENERATE_STOP_DONE
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command: Command Processed
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command: Recieved a Command from Client
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): cmd = 0, Current State 2, Expected State 1
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): OMX_CommandStateSet issued
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Current State 2, Expected State 1
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): Idle-->Loaded-Pending
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command: Command Processed
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): ION: free: handle(0xEB8C3D00), len(2097152), fd(0x41)
04-04 23:11:24.704: E/OMXNodeInstance(217): OMX_FreeBuffer for buffer header 0x43f34670 successful
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): ION: free: handle(0xDCAB0D00), len(2097152), fd(0x3f)
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): ALL input buffers are freed/released
04-04 23:11:24.704: E/OMXNodeInstance(217): OMX_FreeBuffer for buffer header 0x43f34620 successful
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): ION: free: handle(0xE4904BC0), len(368640), fd(0x46)
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): ION: free: handle(0xD31AE040), len(73728), fd(0x44)
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): ALL output buffers are freed/released
04-04 23:11:24.784: E/OMXNodeInstance(217): OMX_FreeBuffer for buffer header 0x43f348f8 successful
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): OMX_CommandStateSet complete, m_state = 1
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): Playback Ended - PASSED
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): ALL output buffers are freed/released
04-04 23:11:24.784: E/OMX-VDEC-1080P(217):  Error in ioctl read next msg
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): omx_vdec: Async thread stop
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): Close the driver instance
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): omx_vdec::component_deinit() complete
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): In OMX vdec Destructor
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): omx_vdec: message thread stop
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): Waiting on OMX Msg Thread exit
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): Waiting on OMX Async Thread exit
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): Exit OMX vdec Destructor

最佳答案

您尝试分配太大的缓冲区(10 * 10241 * 1024 = 104867840 字节)。即使 10241 * 1024 也太大了(10486784 字节)。尝试将其减小到 1024 * 1024 (1048576) 或更小;值得怀疑的是,您是否能够通过网络连接以足够快的速度传输数据,以至于需要这么大的缓冲区。

关于java - 上传文件超过300M时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15820661/

相关文章:

java - 使用 java mail-api 发送电子邮件失败

Android:ListFragment 中 ListView 的项目自身重叠

android - 单击项目时 ListView 特定歌曲播放?

java - android HttpClient 加载许多小缩略图

java - 这部分代码是什么

java - 通过 REST 或 JNDI 使用服务

java - 如何在 Eclipse RCP 应用程序中禁用快速访问 TextField

java - 在模拟器上启动 Android 应用程序?

java - 无法解析符号 Intent - 是否导入 Intent

android - 移动后端在后台处理连续查询