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

9uu*_*uuu 14 android

我通过org.apache.http.client.HttpClientandroid 上传文件,我需要实现进度条.是否有可能从以下方面获取进展:

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);
}
Run Code Online (Sandbox Code Playgroud)

如果没有,请显示另一种方式.谢谢

小智 22

您要做的是创建一个AsyncTask可以为您处理此问题的方法,覆盖onProgressUpdate方法.

这是我在另一个应用程序中使用的测试内容的精简版本HttpURLConnection.可能会有一些小的裁员,我认为HttpURLConnection可能通常不赞成,但这应该有效.只需TheActivity通过调用在您正在使用的任何Activity类中使用此类(在此示例中,我将其称为)new FileUploadTask().execute().当然,您可能需要调整它以满足您的应用程序的需求.

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) {
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

  • 刷新outputStream时将启动实际上载.所以进步是假的进步 (10认同)
  • 在获取OutputStream之前不要忘记:`connection.setDoOutput(true)` (3认同)
  • 这是没有效率的,因为它顺利地进展到100,然后在该对话框实际上文件被转移时被解雇后停留在100%.所以上面的代码中缺少一些东西? (2认同)