将高达50MB的视频上传到服务器

时间:2012-04-23 07:41:39

标签: android

我想从我的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);
            }

3 个答案:

答案 0 :(得分: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) != c语法首先将b分配给a,然后将值与c进行比较(此处:执行read,将结果分配给bytesRead,与-1进行比较)。因此,循环运行直到从流的开始到结束读取每个字节。

read后,buffer中的数据都会通过write写入。由于我们从bytesRead知道buffer中有多少字节实际上是新读取的字节,因此我们告诉write只写入0bytesRead的字节。无需检查InputStream.available()(如果流的长度未知,甚至可能返回无意义的结果)或任何其他方法。

注意:将其更改为while (bytesRead > 0)会带来细微差别。如果您读取0个字节但未到达流的末尾,它将停止读取。这种情况是合法的,尽管假设它没有发生是非常安全的。如果使用bytesRead >= 0,则更安全。

答案 1 :(得分:1)

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

int maxBufferSize = 2*1024*1024;

答案 2 :(得分:0)

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