2

我想将视频从我的 android 应用程序上传到服务器。当我尝试上传视频时,我的应用程序显示
04-23 13:07:41.602: ERROR/dalvikvm-heap(519): Out of memory on a 15607356-byte allocation.我不知道为什么会发生错误。我的问题是我们可以使用下面的代码上传大小高达 50MB 的视频吗?如果有人知道请帮我...

private void doFileUpload(){
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 8*1024*1024;
            Cursor c = (MainscreenActivity.JEEMAHWDroidDB).query((MainscreenActivity.TABLE_Name), new String[] {
                     (MainscreenActivity.COL_HwdXml)}, null, null, null, null,
                              null);
             if(c.getCount()!=0){
             c.moveToLast();
             for(int i=c.getCount()-1; i>=0; i--) {
                  value=c.getString(0);           
             }
             }
            String urlString = value+"/upload_file.php";
            try
            {
             //------------------ CLIENT REQUEST
                UUID uniqueKey = UUID.randomUUID();
                fname = uniqueKey.toString();
                Log.e("UNIQUE NAME",fname);

            FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
            URL url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
            dos = new DataOutputStream( conn.getOutputStream() );
             dos.writeBytes(twoHyphens + boundary + lineEnd);
             dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fname + "."+extension+"" + lineEnd);
             dos.writeBytes(lineEnd);
             bytesAvailable = fileInputStream.available();
             System.out.println("BYTES:--------->"+bytesAvailable);
             bufferSize = Math.min(bytesAvailable, maxBufferSize);
             System.out.println("BUFFER SIZE:--------->"+bufferSize);
             buffer = new byte[bufferSize];
             System.out.println("BUFFER:--------->"+buffer);
             bytesRead = fileInputStream.read(buffer,0,bufferSize);
             System.out.println("BYTES READ:--------->"+bytesRead);
             while (bytesRead > 0)
             {
              dos.write(buffer, 0, bufferSize);
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              System.out.println("RETURNED");
             }
             dos.writeBytes(lineEnd);
             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

             Log.e("Debug","File is written");
             fileInputStream.close();
             dos.flush();
             dos.close();

            }
            catch (MalformedURLException ex)
            {
                 Log.e("Debug", "error: " + ex.getMessage(), ex);
            }
            catch (IOException ioe)
            {
                 Log.e("Debug", "error: " + ioe.getMessage(), ioe);
            }
            //------------------ read the SERVER RESPONSE
            try {
                  inStream = new DataInputStream ( conn.getInputStream() );
                  String str;

                  while (( str = inStream.readLine()) != null)
                  {
                       Log.e("Debug","Server Response "+str);
                  }
                  inStream.close();

            }
            catch (IOException ioex){
             Log.e("Debug", "error: " + ioex.getMessage(), ioex);
            }
4

3 回答 3

3

尝试更换

 bytesAvailable = fileInputStream.available();
 System.out.println("BYTES:--------->"+bytesAvailable);
 bufferSize = Math.min(bytesAvailable, maxBufferSize);
 System.out.println("BUFFER SIZE:--------->"+bufferSize);
 buffer = new byte[bufferSize];
 System.out.println("BUFFER:--------->"+buffer);
 bytesRead = fileInputStream.read(buffer,0,bufferSize);
 System.out.println("BYTES READ:--------->"+bytesRead);
 while (bytesRead > 0)
 {
  dos.write(buffer, 0, bufferSize);
  bytesAvailable = fileInputStream.available();
  bufferSize = Math.min(bytesAvailable, maxBufferSize);
  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  System.out.println("RETURNED");
 }

buffer = new byte[8192];
bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
    dos.write(buf, 0, bytesRead);
}

解释:read()将返回它读取的字节数,或者-1是否到达流的末尾并且无法读取更多数据。它还buffer用它读取的数据填充前 N 个字节。语法首先将(a = b) != cb 分配给 a,然后将值与 c 进行比较(这里:执行read,将结果分配给bytesRead,比较到-1)。因此循环一直运行,直到从流的开始到结束读取每个字节。

在每次写入read数据之后,buffer通过write. 因为我们知道有bytesRead多少字节buffer实际上是新读取的字节,所以我们告诉write只写入来自0to的字节bytesRead。无需检查InputStream.available()(如果流的长度未知,甚至可能返回无意义的结果)或任何其他方法。

注意:将其更改为while (bytesRead > 0)引入了细微的差异。如果您读取 0 个字节但未到达流的末尾,它将停止读取。这种情况是合法的,尽管假设它不会发生是很安全的。如果你使用,你会更安全bytesRead >= 0

于 2012-04-24T12:05:58.927 回答
1

这意味着,您的内存不足。尝试通过减小缓冲区大小来减少我的内存使用量:

int maxBufferSize = 2*1024*1024;
于 2012-04-23T07:53:42.537 回答
0

即使在使用此代码后,您也会遇到相同的错误“内存不足”,在这种情况下,您应该使用 httppost 和多部分实体来发布您的数据......

于 2013-02-19T11:31:58.240 回答