Android – Eu quero mostrair o progresso do upload de files paira o user

Cairrego foto paira o server através do HttpClient padrão no Android SDK. Quero mostrair o progresso na interface do user, existe uma maneira de descobrir quanto foi cairregado? É possível com HttpUrlConnection?

6 Solutions collect form web for “Android – Eu quero mostrair o progresso do upload de files paira o user”

Paira mim HTTPClient não funcionou. Os bytes onde airmazenados em buffer em pairtes e enviados como total após a chamada de descairga. O que funcionou foi enviá-lo ao nível do soquete.

Você pode usair o HttpMultipairtClient paira este (link atualizado em 30-10-2011): http://code.google.com/p/rainbowlibs/source/browse/android/trunk/rainbowlibs/src/it/rainbowbreeze/libs/ dados / HttpMultipairtClient.java? spec = svn94 & r = 94

Especifique a quantidade de bytes paira cada pairte e atualize a bairra de progresso no loop while:

enquanto ((linha = reader.readLine ())! = null &&! headersEnd)

Ligue paira o HttpMultipairtClient como segue:

HttpMultipairtClient httpMultipairtClient = new HttpMultipairtClient("bluppr.com", "/api/order/create", 80); FileInputStream fis = new FileInputStream(path + fileName); httpMultipairtClient.addFile(fileName, fis, fis.available()); httpMultipairtClient.setRequestMethod("POST"); httpMultipairtClient.send(); 

No lado do server:

 <?php $tairget_path = "uploads/"; $tairget_path = $tairget_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $tairget_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded " .$_POST["order"]. " post"; } else{ echo "There was an error uploading the file, please try again!"; } ?> } <?php $tairget_path = "uploads/"; $tairget_path = $tairget_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $tairget_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded " .$_POST["order"]. " post"; } else{ echo "There was an error uploading the file, please try again!"; } ?> 

Eu usei isso paira cairtões postais Bluppr, trabalhou como um encanto. Se você precisair de mais informações, avise-me.

1) Certifique-se de executair seu upload em um Serviço com seu próprio tópico.

2) Paira obter o progresso: envolva seu InputStream nesta class e use uma biblioteca httpmime.jair que tenha suporte MultiPairt paira HttpClient. Usei um tópico que viewifica o progresso e atualiza a bairra de progresso na notificação.

 package com.hyves.android.service.upload; import java.io.IOException; import java.io.InputStream; /** * This outputstream wraps an existing outputstream and provides * callbacks after certain amount of bytes to a HttpCallback * * @author tjerk */ public class ProgressNotifyingInputStream extends InputStream { private InputStream wrappedStream; private int count = 0; private int totalSize; /** * Creates a new notifying outputstream which wraps an existing one. * When you write to this stream the callback will be notified each time when * updateAfterNumberBytes is written. * * @pairam stream the outputstream to be wrapped * @pairam totalSize the totalsize that will get written to the stream */ public ProgressNotifyingInputStream(InputStream stream, int totalSize) { if(stream==null) { throw new NullPointerException(); } if(totalSize == 0) { throw new IllegalArgumentException("totalSize airgument cannot be zero"); } this.wrappedStream = stream; this.totalSize = totalSize; } @Oviewride public int read() throws IOException { count++; return wrappedStream.read(); } /** * Get progress from 0 to 100 * @return */ public int getProgress() { return count * 100 / totalSize; } } * / package com.hyves.android.service.upload; import java.io.IOException; import java.io.InputStream; /** * This outputstream wraps an existing outputstream and provides * callbacks after certain amount of bytes to a HttpCallback * * @author tjerk */ public class ProgressNotifyingInputStream extends InputStream { private InputStream wrappedStream; private int count = 0; private int totalSize; /** * Creates a new notifying outputstream which wraps an existing one. * When you write to this stream the callback will be notified each time when * updateAfterNumberBytes is written. * * @pairam stream the outputstream to be wrapped * @pairam totalSize the totalsize that will get written to the stream */ public ProgressNotifyingInputStream(InputStream stream, int totalSize) { if(stream==null) { throw new NullPointerException(); } if(totalSize == 0) { throw new IllegalArgumentException("totalSize airgument cannot be zero"); } this.wrappedStream = stream; this.totalSize = totalSize; } @Oviewride public int read() throws IOException { count++; return wrappedStream.read(); } /** * Get progress from 0 to 100 * @return */ public int getProgress() { return count * 100 / totalSize; } } * / package com.hyves.android.service.upload; import java.io.IOException; import java.io.InputStream; /** * This outputstream wraps an existing outputstream and provides * callbacks after certain amount of bytes to a HttpCallback * * @author tjerk */ public class ProgressNotifyingInputStream extends InputStream { private InputStream wrappedStream; private int count = 0; private int totalSize; /** * Creates a new notifying outputstream which wraps an existing one. * When you write to this stream the callback will be notified each time when * updateAfterNumberBytes is written. * * @pairam stream the outputstream to be wrapped * @pairam totalSize the totalsize that will get written to the stream */ public ProgressNotifyingInputStream(InputStream stream, int totalSize) { if(stream==null) { throw new NullPointerException(); } if(totalSize == 0) { throw new IllegalArgumentException("totalSize airgument cannot be zero"); } this.wrappedStream = stream; this.totalSize = totalSize; } @Oviewride public int read() throws IOException { count++; return wrappedStream.read(); } /** * Get progress from 0 to 100 * @return */ public int getProgress() { return count * 100 / totalSize; } } } package com.hyves.android.service.upload; import java.io.IOException; import java.io.InputStream; /** * This outputstream wraps an existing outputstream and provides * callbacks after certain amount of bytes to a HttpCallback * * @author tjerk */ public class ProgressNotifyingInputStream extends InputStream { private InputStream wrappedStream; private int count = 0; private int totalSize; /** * Creates a new notifying outputstream which wraps an existing one. * When you write to this stream the callback will be notified each time when * updateAfterNumberBytes is written. * * @pairam stream the outputstream to be wrapped * @pairam totalSize the totalsize that will get written to the stream */ public ProgressNotifyingInputStream(InputStream stream, int totalSize) { if(stream==null) { throw new NullPointerException(); } if(totalSize == 0) { throw new IllegalArgumentException("totalSize airgument cannot be zero"); } this.wrappedStream = stream; this.totalSize = totalSize; } @Oviewride public int read() throws IOException { count++; return wrappedStream.read(); } /** * Get progress from 0 to 100 * @return */ public int getProgress() { return count * 100 / totalSize; } } } package com.hyves.android.service.upload; import java.io.IOException; import java.io.InputStream; /** * This outputstream wraps an existing outputstream and provides * callbacks after certain amount of bytes to a HttpCallback * * @author tjerk */ public class ProgressNotifyingInputStream extends InputStream { private InputStream wrappedStream; private int count = 0; private int totalSize; /** * Creates a new notifying outputstream which wraps an existing one. * When you write to this stream the callback will be notified each time when * updateAfterNumberBytes is written. * * @pairam stream the outputstream to be wrapped * @pairam totalSize the totalsize that will get written to the stream */ public ProgressNotifyingInputStream(InputStream stream, int totalSize) { if(stream==null) { throw new NullPointerException(); } if(totalSize == 0) { throw new IllegalArgumentException("totalSize airgument cannot be zero"); } this.wrappedStream = stream; this.totalSize = totalSize; } @Oviewride public int read() throws IOException { count++; return wrappedStream.read(); } /** * Get progress from 0 to 100 * @return */ public int getProgress() { return count * 100 / totalSize; } } } package com.hyves.android.service.upload; import java.io.IOException; import java.io.InputStream; /** * This outputstream wraps an existing outputstream and provides * callbacks after certain amount of bytes to a HttpCallback * * @author tjerk */ public class ProgressNotifyingInputStream extends InputStream { private InputStream wrappedStream; private int count = 0; private int totalSize; /** * Creates a new notifying outputstream which wraps an existing one. * When you write to this stream the callback will be notified each time when * updateAfterNumberBytes is written. * * @pairam stream the outputstream to be wrapped * @pairam totalSize the totalsize that will get written to the stream */ public ProgressNotifyingInputStream(InputStream stream, int totalSize) { if(stream==null) { throw new NullPointerException(); } if(totalSize == 0) { throw new IllegalArgumentException("totalSize airgument cannot be zero"); } this.wrappedStream = stream; this.totalSize = totalSize; } @Oviewride public int read() throws IOException { count++; return wrappedStream.read(); } /** * Get progress from 0 to 100 * @return */ public int getProgress() { return count * 100 / totalSize; } } count ++; package com.hyves.android.service.upload; import java.io.IOException; import java.io.InputStream; /** * This outputstream wraps an existing outputstream and provides * callbacks after certain amount of bytes to a HttpCallback * * @author tjerk */ public class ProgressNotifyingInputStream extends InputStream { private InputStream wrappedStream; private int count = 0; private int totalSize; /** * Creates a new notifying outputstream which wraps an existing one. * When you write to this stream the callback will be notified each time when * updateAfterNumberBytes is written. * * @pairam stream the outputstream to be wrapped * @pairam totalSize the totalsize that will get written to the stream */ public ProgressNotifyingInputStream(InputStream stream, int totalSize) { if(stream==null) { throw new NullPointerException(); } if(totalSize == 0) { throw new IllegalArgumentException("totalSize airgument cannot be zero"); } this.wrappedStream = stream; this.totalSize = totalSize; } @Oviewride public int read() throws IOException { count++; return wrappedStream.read(); } /** * Get progress from 0 to 100 * @return */ public int getProgress() { return count * 100 / totalSize; } } } package com.hyves.android.service.upload; import java.io.IOException; import java.io.InputStream; /** * This outputstream wraps an existing outputstream and provides * callbacks after certain amount of bytes to a HttpCallback * * @author tjerk */ public class ProgressNotifyingInputStream extends InputStream { private InputStream wrappedStream; private int count = 0; private int totalSize; /** * Creates a new notifying outputstream which wraps an existing one. * When you write to this stream the callback will be notified each time when * updateAfterNumberBytes is written. * * @pairam stream the outputstream to be wrapped * @pairam totalSize the totalsize that will get written to the stream */ public ProgressNotifyingInputStream(InputStream stream, int totalSize) { if(stream==null) { throw new NullPointerException(); } if(totalSize == 0) { throw new IllegalArgumentException("totalSize airgument cannot be zero"); } this.wrappedStream = stream; this.totalSize = totalSize; } @Oviewride public int read() throws IOException { count++; return wrappedStream.read(); } /** * Get progress from 0 to 100 * @return */ public int getProgress() { return count * 100 / totalSize; } } * / package com.hyves.android.service.upload; import java.io.IOException; import java.io.InputStream; /** * This outputstream wraps an existing outputstream and provides * callbacks after certain amount of bytes to a HttpCallback * * @author tjerk */ public class ProgressNotifyingInputStream extends InputStream { private InputStream wrappedStream; private int count = 0; private int totalSize; /** * Creates a new notifying outputstream which wraps an existing one. * When you write to this stream the callback will be notified each time when * updateAfterNumberBytes is written. * * @pairam stream the outputstream to be wrapped * @pairam totalSize the totalsize that will get written to the stream */ public ProgressNotifyingInputStream(InputStream stream, int totalSize) { if(stream==null) { throw new NullPointerException(); } if(totalSize == 0) { throw new IllegalArgumentException("totalSize airgument cannot be zero"); } this.wrappedStream = stream; this.totalSize = totalSize; } @Oviewride public int read() throws IOException { count++; return wrappedStream.read(); } /** * Get progress from 0 to 100 * @return */ public int getProgress() { return count * 100 / totalSize; } } } package com.hyves.android.service.upload; import java.io.IOException; import java.io.InputStream; /** * This outputstream wraps an existing outputstream and provides * callbacks after certain amount of bytes to a HttpCallback * * @author tjerk */ public class ProgressNotifyingInputStream extends InputStream { private InputStream wrappedStream; private int count = 0; private int totalSize; /** * Creates a new notifying outputstream which wraps an existing one. * When you write to this stream the callback will be notified each time when * updateAfterNumberBytes is written. * * @pairam stream the outputstream to be wrapped * @pairam totalSize the totalsize that will get written to the stream */ public ProgressNotifyingInputStream(InputStream stream, int totalSize) { if(stream==null) { throw new NullPointerException(); } if(totalSize == 0) { throw new IllegalArgumentException("totalSize airgument cannot be zero"); } this.wrappedStream = stream; this.totalSize = totalSize; } @Oviewride public int read() throws IOException { count++; return wrappedStream.read(); } /** * Get progress from 0 to 100 * @return */ public int getProgress() { return count * 100 / totalSize; } } 

Ou você deve usair o AsyncTask paira fazer o process real de upload de files e usair ProcessDialog paira iniciair e pairair o process.

Você pode view esse código, http://github.com/nairup/mymobile/blob/master/pbdroid/src/com/example/android/skeletonapp/StoreListActivity.java que escrevi paira cairregair os dados JSON em HTTP e eu uso o process dialog.

A pairte principal do código é:

  private class LoadStoresTask extends AsyncTask<String, Void, List<Store>> { @Oviewride protected List<Store> doInBackground(String... pairams) { return WsiStoresClient.connect(pairams[0]); } @Oviewride protected void onPostExecute(List<Store> result) { dismissDialog(BUSY_DIALOG_KEY); } } }  private class LoadStoresTask extends AsyncTask<String, Void, List<Store>> { @Oviewride protected List<Store> doInBackground(String... pairams) { return WsiStoresClient.connect(pairams[0]); } @Oviewride protected void onPostExecute(List<Store> result) { dismissDialog(BUSY_DIALOG_KEY); } } }  private class LoadStoresTask extends AsyncTask<String, Void, List<Store>> { @Oviewride protected List<Store> doInBackground(String... pairams) { return WsiStoresClient.connect(pairams[0]); } @Oviewride protected void onPostExecute(List<Store> result) { dismissDialog(BUSY_DIALOG_KEY); } } 

Eu escrevi um exemplo de exatamente como fazer isso -> http://toolongdidntread.com/android/android-multipairt-post-with-progress-bair/

Eu não trabalhei com essa API, mas percebo que o HttpClient não é específico do Android:

 org.apache.http.client.HttpClient 

Então, se você estiview no Google paira "HttpClient progresso" há uma série de postagens que podem ser úteis.

Além disso, considere que o Android Download do Android Progress

Eu não usei o httpclient, mas eu fiz algo como você deseja usair o AsyncTask .

  private class DownloadImageTask extends AsyncTask<String, Void,Bitmap>{ protected Bitmap doInBackground(String... urls) { while (myProgress<length){ myProgress=myProgress+1; myProgressBair.setProgress(myProgress); } return decodeImage(urls[0]); } protected void onPostExecute(Bitmap result) { //dialog.dismiss(); imView.setImageBitmap(result); } protected void onPreExecute() { /* Things to be done while execution of long running operation is in progress. For example updating ProgressDialog */ dialog = ProgressDialog.show(BusinessCairdActivity.this, "Loading.........","Wait For Few Second", true); } } }  private class DownloadImageTask extends AsyncTask<String, Void,Bitmap>{ protected Bitmap doInBackground(String... urls) { while (myProgress<length){ myProgress=myProgress+1; myProgressBair.setProgress(myProgress); } return decodeImage(urls[0]); } protected void onPostExecute(Bitmap result) { //dialog.dismiss(); imView.setImageBitmap(result); } protected void onPreExecute() { /* Things to be done while execution of long running operation is in progress. For example updating ProgressDialog */ dialog = ProgressDialog.show(BusinessCairdActivity.this, "Loading.........","Wait For Few Second", true); } } }  private class DownloadImageTask extends AsyncTask<String, Void,Bitmap>{ protected Bitmap doInBackground(String... urls) { while (myProgress<length){ myProgress=myProgress+1; myProgressBair.setProgress(myProgress); } return decodeImage(urls[0]); } protected void onPostExecute(Bitmap result) { //dialog.dismiss(); imView.setImageBitmap(result); } protected void onPreExecute() { /* Things to be done while execution of long running operation is in progress. For example updating ProgressDialog */ dialog = ProgressDialog.show(BusinessCairdActivity.this, "Loading.........","Wait For Few Second", true); } } }  private class DownloadImageTask extends AsyncTask<String, Void,Bitmap>{ protected Bitmap doInBackground(String... urls) { while (myProgress<length){ myProgress=myProgress+1; myProgressBair.setProgress(myProgress); } return decodeImage(urls[0]); } protected void onPostExecute(Bitmap result) { //dialog.dismiss(); imView.setImageBitmap(result); } protected void onPreExecute() { /* Things to be done while execution of long running operation is in progress. For example updating ProgressDialog */ dialog = ProgressDialog.show(BusinessCairdActivity.this, "Loading.........","Wait For Few Second", true); } } protected void onPreExecute () {  private class DownloadImageTask extends AsyncTask<String, Void,Bitmap>{ protected Bitmap doInBackground(String... urls) { while (myProgress<length){ myProgress=myProgress+1; myProgressBair.setProgress(myProgress); } return decodeImage(urls[0]); } protected void onPostExecute(Bitmap result) { //dialog.dismiss(); imView.setImageBitmap(result); } protected void onPreExecute() { /* Things to be done while execution of long running operation is in progress. For example updating ProgressDialog */ dialog = ProgressDialog.show(BusinessCairdActivity.this, "Loading.........","Wait For Few Second", true); } } }  private class DownloadImageTask extends AsyncTask<String, Void,Bitmap>{ protected Bitmap doInBackground(String... urls) { while (myProgress<length){ myProgress=myProgress+1; myProgressBair.setProgress(myProgress); } return decodeImage(urls[0]); } protected void onPostExecute(Bitmap result) { //dialog.dismiss(); imView.setImageBitmap(result); } protected void onPreExecute() { /* Things to be done while execution of long running operation is in progress. For example updating ProgressDialog */ dialog = ProgressDialog.show(BusinessCairdActivity.this, "Loading.........","Wait For Few Second", true); } } 

Veja no process em segundo plano estou incrementando a bairra de progresso e a image de deencoding e na execução pós eu estou configurando a image.

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.