【问题标题】:Android HttpsURLConnection on 2.3.4 OutOfMemoryError while uploading Large File (byte[] in memory)Android HttpsURLConnection on 2.3.4 OutOfMemoryError while uploading Large File (byte[] in memory)
【发布时间】:2013-04-03 21:50:38
【问题描述】:

我正在尝试在服务器上上传约 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();

【问题讨论】:

    标签: android out-of-memory httpurlconnection large-files


    【解决方案1】:

    我向 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

    我最终将 HttpClient 用于 EclairFroyoGingerBreadHoneyComb 及以上版本的 HttpURLConnection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-18
      • 2020-05-08
      • 2020-01-31
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2023-03-18
      • 2011-07-25
      相关资源
      最近更新 更多