I Need Know How Much Bytes Has Been Uploaded For Update A Progress Bar Android
Solution 1:
Have you tried extending FileBody? Presumably the POST will either call getInputStream()
or writeTo()
in order to actually send the file data to the server. You could extend either of these (including the InputStream returned by getInputStream()
) and keep track of how much data has been sent.
Solution 2:
thank to cyngus's idea I have resolved this issue. I have added the next code for tracking the uploaded bytes:
Listener on upload button:
btnSubir.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
//pd = ProgressDialog.show(VideoAndroidActivity.this, "", "Subiendo Video", true, false);
pd = newProgressDialog(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();newUploadVideo().execute();
}
});
Asynctask for run the upload:
classUploadVideoextendsAsyncTask<Void,Integer,String> {
private FileBody fb;
@Overrideprotected String doInBackground(Void... params) {
// Create a new HttpClient and Post HeaderHttpClienthttpclient=newDefaultHttpClient();
HttpPosthttppost=newHttpPost("http://www.youtouch.cl/videoloader/index.php");
int count;
try {
// Add your data
File input=newFile(fileName);
// I created a Filebody Object
fb=newFileBody(input);
MultipartEntity multi=newMultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multi.addPart("video",fb);
httppost.setEntity(multi);
// Execute HTTP Post RequestHttpResponseresponse= httpclient.execute(httppost);
//get the InputStream
InputStream is=fb.getInputStream();
//create a bufferbyte data[] = newbyte[1024];//1024//this var updates the progress barlong total=0;
while((count=is.read(data))!=-1){
total+=count;
publishProgress((int)(total*100/input.length()));
}
is.close();
HttpEntityentity= response.getEntity();
BufferedReaderreader=newBufferedReader(
newInputStreamReader(
entity.getContent(), "UTF-8"));
StringsResponse= 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";
}
@OverrideprotectedvoidonProgressUpdate(Integer... unsued) {
pd.setProgress(unsued[0]);
}
@OverrideprotectedvoidonPostExecute(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);
}
}
}
The progress bar load is bit slow (in starting seems be freeze and then load of 1 to 100 very fast), but works.
Sorry, my english is regular :(.
Solution 3:
Check my answer here, I guess it answers your question: But update the file path of the image to your to be uploaded video
https://stackoverflow.com/questions/15572747/progressbar-in-asynctask-is-not-showing-on-upload
Solution 4:
What I used to do is to extends org.apache.http.entity.ByteArrayEntity and override the writeTo function like below, while bytes output it will pass though writeTo(), so you can count current output bytes:
@OverridepublicvoidwriteTo(final OutputStream outstream)throws IOException
{
if (outstream == null) {
thrownewIllegalArgumentException("Output stream may not be null");
}
InputStreaminstream=newByteArrayInputStream(this.content);
try {
byte[] tmp = newbyte[512];
inttotal= (int) this.content.length;
intprogress=0;
intincrement=0;
int l;
int percent;
// read file and write to http output streamwhile ((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 incrementif (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();
}
}
Post a Comment for "I Need Know How Much Bytes Has Been Uploaded For Update A Progress Bar Android"