2011-07-27 13 views
4

Apache/PHPサーバーへのアップロードビデオ用のアプリケーションを開発しています。この瞬間、私は既にビデオをアップロードできます。ファイルがアップロードされている間に進行状況バーを表示する必要があります。私は次のコードAsyncTaskとHTTP 4.1.1を使用して、フォームをエミュレートするためのライブラリを持っています。進捗バーを更新するためにアップロードされたバイト数を知る必要があります。

class uploadVideo extends AsyncTask<Void,Void,String>{ 

    @Override 
    protected String doInBackground(Void... params) { 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://www.youtouch.cl/videoloader/index.php");   
     try { 
      // Add your data 
      File input=new File(fileName);    

      MultipartEntity multi=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);     

      multi.addPart("video", new FileBody(input));     

      httppost.setEntity(multi); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost);    

      HttpEntity entity = response.getEntity(); 

      BufferedReader reader = new BufferedReader(
        new InputStreamReader(
          entity.getContent(), "UTF-8")); 
      String sResponse = reader.readLine(); 
      return sResponse; 

     } catch (ClientProtocolException e) { 
      Log.v("Uri Galeria", e.toString()); 
      e.printStackTrace();     

     } catch (IOException e) { 
      Log.v("Uri Galeria", e.toString()); 
      e.printStackTrace();     
     } 
     return "error"; 
    } 

    @Override 
    protected void onProgressUpdate(Void... unsued) { 
       //Here I do should update the progress bar 
    } 

    @Override 
    protected void onPostExecute(String sResponse) { 
     try { 
      if (pd.isShowing()) 
       pd.dismiss(); 

      if (sResponse != null) { 
       JSONObject JResponse = new JSONObject(sResponse); 
       int success = JResponse.getInt("SUCCESS"); 
       String message = JResponse.getString("MESSAGE"); 
       if (success == 0) { 
        Toast.makeText(getApplicationContext(), message, 
          Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(getApplicationContext(), 
          "Video uploaded successfully", 
          Toast.LENGTH_SHORT).show(); 

       } 
      } 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), 
        e.getMessage(), 
        Toast.LENGTH_LONG).show(); 
      Log.e(e.getClass().getName(), e.getMessage(), e); 
     } 
    } 

どのくらいのバイトがアップロードされたかを知る必要があります。 File.lengthは合計サイズです。

答えて

3

FileBodyを拡張しようとしましたか?おそらくPOSTは、実際にファイルデータをサーバーに送信するために、getInputStream()またはwriteTo()を呼び出します。これらのいずれか(getInputStream()によって返されたInputStreamを含む)を拡張し、送信されたデータの量を記録することができます。

+0

私はあなたの答えを試してみます。ご挨拶! – ClarkXP

+0

投稿時にFileBody.writeTo()を呼び出します。 – Octavian

3

私はこの問題を解決しました。アップロードボタンを

リスナー:

btnSubir.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      //pd = ProgressDialog.show(VideoAndroidActivity.this, "", "Subiendo Video", true, false); 

      pd = new ProgressDialog(VideoAndroidActivity.this); 
      pd.setMessage("Uploading Video"); 
      pd.setIndeterminate(false); 
      pd.setMax(100); 
      pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      pd.show(); 
      //Thread thread=new Thread(new threadUploadVideo()); 
      //thread.start(); 
      new UploadVideo().execute(); 
     } 
    }); 

Asynctaskアップロードを実行するために:私は、アップロードされたバイト数を追跡​​するための次のコードを追加した

class UploadVideo extends AsyncTask<Void,Integer,String> { 
    private FileBody fb; 

    @Override 
    protected String doInBackground(Void... params) { 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://www.youtouch.cl/videoloader/index.php"); 
     int count; 
     try { 
      // Add your data 
      File input=new File(fileName); 

      // I created a Filebody Object 
      fb=new FileBody(input); 
      MultipartEntity multi=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
      multi.addPart("video",fb);   

      httppost.setEntity(multi);    
      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 

      //get the InputStream 
      InputStream is=fb.getInputStream(); 

      //create a buffer 
      byte data[] = new byte[1024];//1024 

      //this var updates the progress bar 
      long total=0; 
      while((count=is.read(data))!=-1){ 
       total+=count; 
       publishProgress((int)(total*100/input.length())); 
      } 
      is.close();    
      HttpEntity entity = response.getEntity(); 

      BufferedReader reader = new BufferedReader(
        new InputStreamReader(
          entity.getContent(), "UTF-8")); 
      String sResponse = reader.readLine(); 
      return sResponse; 

     } catch (ClientProtocolException e) { 
      Log.v("Uri Galeria", e.toString()); 
      e.printStackTrace();     

     } catch (IOException e) { 
      Log.v("Uri Galeria", e.toString()); 
      e.printStackTrace();     
     } 
     return "error"; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... unsued) {   
     pd.setProgress(unsued[0]); 
    } 

    @Override 
    protected void onPostExecute(String sResponse) { 
     try { 
      if (pd.isShowing()) 
       pd.dismiss(); 

      if (sResponse != null) { 
        Toast.makeText(getApplicationContext(),sResponse,Toast.LENGTH_SHORT).show(); 
        Log.i("Splash", sResponse);     
      } 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), 
        e.getMessage(), 
        Toast.LENGTH_LONG).show(); 
      Log.e(e.getClass().getName(), e.getMessage(), e); 
     } 
    } 


} 

プログレスバーの負荷が少し遅いです(中開始はフリーズしてから1から100まで非常に速くなります)、動作します。

申し訳ありませんが、私の英語は、正規:(ある。

+1

これは解決策ではありませんでした:(ビデオロードがプログレスバープロセスと関連していないため、このコードでは最初にビデオが読み込まれ、プログレスバーはパラレルではなく – ClarkXP

+0

実際にこの問題を解決しましたあなたが答えるなら、私にタグをつけてください:) –

1

私は何を使用すると、org.apache.http.entity.ByteArrayEntityを拡張し、バイト出力は、それがのwriteToかかわらず通過しますが、以下のようなのwriteTo機能をオーバーライドすることです()は、現在の出力バイト数えることができるように:ここに私の答えをチェックし、私はそれは、あなたの質問に答えると思い

@Override 
public void writeTo(final OutputStream outstream) throws IOException 
{ 
    if (outstream == null) { 
     throw new IllegalArgumentException("Output stream may not be null"); 
    } 

    InputStream instream = new ByteArrayInputStream(this.content); 

    try { 
     byte[] tmp = new byte[512]; 
     int total = (int) this.content.length; 
     int progress = 0; 
     int increment = 0; 
     int l; 
     int percent; 

     // read file and write to http output stream 
     while ((l = instream.read(tmp)) != -1) { 
      // check progress 
      progress = progress + l; 
      percent = Math.round(((float) progress/(float) total) * 100); 

      // if percent exceeds increment update status notification 
      // and adjust increment 
      if (percent > increment) { 
       increment += 10; 
       // update percentage here !! 
      } 

      // write to output stream 
      outstream.write(tmp, 0, l); 
     } 

     // flush output stream 
     outstream.flush(); 
    } finally { 
     // close input stream 
     instream.close(); 
    } 
} 
関連する問題