2015-07-21 5 views
2

J'ai une méthode doFileUpload dans UploadAsync classe que extends AsyncTask. Je veux savoir comment calculer le téléchargement de pourcentage de la tâche à partir du code ci-dessous pour mettre à jour un ProgressDialog en utilisant publishProgress(""+values)Comment calculer le pourcentage de la tâche de téléchargement

private void doFileUpload(){ 
     HttpURLConnection conn = null; 
     DataOutputStream dos = null; 
     DataInputStream inStream = null; 
     String exsistingFileName = "/sdcard/six.3gp"; 
     // Is this the place are you doing something wrong. 
     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 
     int bytesRead, bytesAvailable, bufferSize; 
     byte[] buffer; 
     int maxBufferSize = 1*1024*1024; 
     String urlString = "http://192.168.1.5/upload.php"; 
     try 
     { 
      Log.e("MediaPlayer","Inside second Method"); 
      FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName)); 
      URL url = new URL(urlString); 
      conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoInput(true); 
      // Allow Outputs 
      conn.setDoOutput(true); 
      // Don't use a cached copy. 
      conn.setUseCaches(false); 
      // Use a post method. 
      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=\"" + exsistingFileName +"\"" + lineEnd); 
      dos.writeBytes(lineEnd); 
      Log.e("MediaPlayer","Headers are written"); 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      buffer = new byte[bufferSize]; 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      while (bytesRead > 0) 
      { 
       dos.write(buffer, 0, bufferSize); 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      } 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      String inputLine; 
      while ((inputLine = in.readLine()) != null) 
       tv.append(inputLine); 
      // close streams 
      Log.e("MediaPlayer","File is written"); 
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 
     } 
     catch (MalformedURLException ex) 
     { 
      Log.e("MediaPlayer", "error: " + ex.getMessage(), ex); 
     } 
     catch (IOException ioe) 
     { 
      Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe); 
     } 

     //------------------ read the SERVER RESPONSE 
     try { 
      inStream = new DataInputStream (conn.getInputStream()); 
      String str;    
      while ((str = inStream.readLine()) != null) 
      { 
       Log.e("MediaPlayer","Server Response"+str); 
      } 
      /*while((str = inStream.readLine()) !=null){ 

      }*/ 
      inStream.close(); 
     } 
     catch (IOException ioex){ 
      Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); 
     } 
    } 

Merci

Répondre

0

voilà comment je l'ai fait dans ma demande

long total = 0; 
int lenghtOfFile=connection.getContentLength(); 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       // After this onProgressUpdate will be called 
       publishProgress(""+(int)((total*100)/lenghtOfFile)); 
       // writing data to file 
       output.write(data, 0, count); 
       if (isCancelled()) break; 
      } 

Vous pouvez le voir ici: The Official documentation

+0

ici 'HttpURLConnection connection = null;' – Shekhar