tags:

views:

1345

answers:

6

I upload photo to the server via the default HttpClient in Android SDK. I want to show progress in the user interface, is there a way to find out how much has been uploaded? Is it possible with HttpUrlConnection?

A: 

I did not work with that API, but notice that HttpClient is not android specific:

org.apache.http.client.HttpClient

So if you google for "HttpClient progress" there is a number of posts that may be usefull.

Also, consider that post http://stackoverflow.com/questions/2100737/android-download-progress

alex2k8
+1  A: 

Or you should use AsyncTask to do the actual process of file upload and use ProcessDialog to start and stop the process.

You can see this code, http://github.com/narup/mymobile/blob/master/pbdroid/src/com/example/android/skeletonapp/StoreListActivity.java i wrote to load the JSON data over HTTP and i use process dialog.

Main part of the code is :

 private class LoadStoresTask extends AsyncTask<String, Void, List<Store>> {

@Override
protected List<Store> doInBackground(String... params) {
return WsiStoresClient.connect(params[0]);
}

@Override
protected void onPostExecute(List<Store> result) {
dismissDialog(BUSY_DIALOG_KEY);
}

}
narup
A: 

i haven't used httpclient but i have done something like you want using asyntask.

private class DownloadImageTask extends AsyncTask { protected Bitmap doInBackground(String... urls) {

          while (myProgress<length){
                 myProgress=myProgress+1;  
                 myProgressBar.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 ProgessDialog // dialog = ProgressDialog.show(BusinessCardActivity.this, "Loading.........", // "Wait For Few Second", true); } }

see in background process i am incrementing progressbar and decoding image and in post execution i am setting image.

ud_an
This will show the progress for a bunch of images... not for a single image.
Cristian
A: 

will you please share your code to upload picture to server and the server code to accept that file ? i am looking for this kind of code from weeks but have not find any good working code :(

Andrew
That area must be used to write answers only... use the comment form to post *comments*
Cristian
+3  A: 

For me HTTPClient didn't work. The bytes where buffered in parts and sent as total after the flush call. What worked was to sent it on socket level.

You can use the HttpMultipartClient for this: http://www.listware.net/201004/android-developers/100508-android-developers-re-uploading-large-files-using-setchunkedstreamingmode.html

Specify the amount of bytes for eatch part and update the progressbar in the while loop:

while (( line = reader.readLine()) != null && !headersEnd)

Call the HttpMultipartClient as folow:

HttpMultipartClient httpMultipartClient = new HttpMultipartClient("v2.bluppr.nl", "/webservice/placeorder", 80);

FileInputStream fis = new FileInputStream(path + fileName);
httpMultipartClient.addFile(fileName, fis, fis.available());
httpMultipartClient.setRequestMethod("POST");
httpMultipartClient.send();

At the server side use:

<?php

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_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!";
}

?>

I used this for Bluppr Postcards, worked like a charm. If you need more info let me know.

Ben
A: 

I found something that works over wifi but I use HTTPCLIENT and it corrupts the upload on 3g ??

Its driving me crazy, if anyone has any ideas why

Anthony