如何实现在Android文件上传进度条 [英] How to implement file upload progress bar in android
问题描述
我通过上传 org.apache.http.client.HttpClient
文件在Android和我需要实施进度条。是否有可能抓住从进展情况:?
I am uploading files via org.apache.http.client.HttpClient
in android and I need implement progress bar. Is it possible to grab progress from: ?
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);
}
如果没有,请出示的另一种方式。谢谢
If not, please show an alternative way. Thanks
推荐答案
你会怎么想要做的就是创建一个的AsyncTask
,可以处理这个给你,覆盖onProgressUpdate方法。
What you're going to want to do is create an AsyncTask
that can handle this for you, overriding the onProgressUpdate method.
这里有一个东西,我在另一个应用程序的HttpURLConnection
测试使用的是简化版。可能会有一些小的裁员,我认为的HttpURLConnection
通常可以令人难以接受的,但这应该工作。只要使用这个类的任何活动类,你正在使用的(在这个例子中,我把它称为 TheActivity
)通过调用新FileUploadTask()。执行()
。当然,你可能需要调整这个以适合你的应用程序的需求。
Here's a stripped down version of a something I tested in another app using HttpURLConnection
. There may be some small redundancies and I think HttpURLConnection
may be generally frowned upon, but this should work. Just use this class in whatever Activity class you're using (in this example, I refer to it as TheActivity
) by calling new FileUploadTask().execute()
. Of course you may need to tweak this to fit the needs of your app.
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) {
}
}
}
这篇关于如何实现在Android文件上传进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!