如何在android中实现文件上传进度条

问题描述:

我在android中通过org.apache.http.client.HttpClient上传文件,我需要实现进度条。是否有可能从以下方面获得进展?如何在android中实现文件上传进度条

HttpPost httppost = new HttpPost("some path"); 
HttpClient httpclient = new DefaultHttpClient(); 
try { 
File file = new File("file path"); 
InputStream in = new BufferedInputStream(new FileInputStream(file)); 
byte[] bArray = new byte[(int) file.length()]; 
in.read(bArray); 
String entity = Base64.encodeToString(bArray, Base64.DEFAULT); 
httppost.setEntity(new StringEntity(entity)); 
HttpResponse response = httpclient.execute(httppost); 
} 

如果不是,请展示另一种方法。谢谢

+2

这篇文章可能是你正在寻找:[Java FileUpload with progress](http://*.com/questions/254719/file-upload-with-java-with-progress-bar)。 –

你要做的是创建一个AsyncTask,可以为你处理这个问题,覆盖onProgressUpdate方法。

这是我在另一个应用程序中使用HttpURLConnection测试过的东西的精简版本。可能会有一些小的冗余,我认为HttpURLConnection可能通常会被人忽视,但这应该起作用。只需通过调用new FileUploadTask().execute()将此类用于您正在使用的任何活动类(在此示例中,我将其称为TheActivity)。当然你可能需要调整这个以适应你的应用程序的需求。

private class FileUploadTask extends AsyncTask<Object, Integer, Void> { 

    private ProgressDialog dialog; 

    @Override 
    protected void onPreExecute() { 
     dialog = new ProgressDialog(TheActivity.this); 
     dialog.setMessage("Uploading..."); 
     dialog.setIndeterminate(false); 
     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     dialog.setProgress(0); 
     dialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Object... arg0) { 
     try { 
      File file = new File("file path"); 
      FileInputStream fileInputStream = new FileInputStream(file); 
      byte[] bytes = new byte[(int) file.length()]; 
      fileInputStream.read(bytes); 
      fileInputStream.close(); 

      URL url = new URL("some path"); 
      HttpURLConnection connection = 
        (HttpURLConnection) url.openConnection(); 
      OutputStream outputStream = connection.getOutputStream(); 

      int bufferLength = 1024; 
      for (int i = 0; i < bytes.length; i += bufferLength) { 
       int progress = (int)((i/(float) bytes.length) * 100); 
       publishProgress(progress); 
       if (bytes.length - i >= bufferLength) { 
        outputStream.write(bytes, i, bufferLength); 
       } else { 
        outputStream.write(bytes, i, bytes.length - i); 
       } 
      } 
      publishProgress(100); 

      outputStream.close(); 
      outputStream.flush(); 

      InputStream inputStream = connection.getInputStream(); 
      // read the response 
      inputStream.close(); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

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

    @Override 
    protected void onPostExecute(Void result) { 
     try { 
      dialog.dismiss(); 
     } catch(Exception e) { 
     } 

    } 

} 
+1

这个效率并不高,因为它直到100进展顺利,然后在该对话实际上被传输时取消关闭之后卡住了100%。所以上面的代码中缺少一些东西? – Hunt

+8

当您刷新outputStream时,实际上传将开始。 所以进度是虚假的进展 – Basbous

+3

不要忘记:获得OutputStream之前的'connection.setDoOutput(true)'! – Nolesh

我不认为HttpURLConnection类简单的伎俩,如@Basbous指出,实际数据bufferred直到outputStream.flush()被调用。根据android issue 3164,它现在修复在post-froyo平台(android 2.2,sdk version 8)中,您需要使用-java.net.HttpURLConnection.setFixedLengthStreamingMode来解决缓冲区行为。