Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - uploading video upto 50MB to the server

I want upload videos from my android apps to server.When iam trying to upload the video my apps shows
04-23 13:07:41.602: ERROR/dalvikvm-heap(519): Out of memory on a 15607356-byte allocation.I dont know why the error is occuring.My question is Can we upload video of size upto 50MB using the below code?if anybody knows please help me...

private void doFileUpload(){
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            String lineEnd = "
";
            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);
            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try replacing

 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");
 }

with

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

Explained: read() will return you the number of bytes it read or -1 if the end of the stream is reached and no further data can be read. It also fills the first N bytes of buffer with the data it read. The (a = b) != c syntax first assigns b to a, then compares the value to c (here: execute read, assign result to bytesRead, compare to -1). Therefore the loop runs until every byte is read from start to the end of the stream.

After each read the data in buffer is written via write. Since we know from bytesRead how much bytes in buffer are actually newly read bytes we tell write to write only the bytes from 0 to bytesRead. No need to check InputStream.available() (which may even return meaningless results if the length of the stream is not known) or any other method.

Note: changing that to while (bytesRead > 0) introduces a subtle difference. It will stop reading if you read 0 bytes but did not reach the end of the stream. That case is legal although it is pretty safe to assume that it does not happen. You are safer if you use bytesRead >= 0 though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...