I have a scenario, In my chat application I want to send photos, photos sending takes time. I have attached a progress bar( horizontal) to show the progress of sending the photo in my application. But I am unable to update the progress bar as it is the item of the custom list view.
Here is my asynchtask to set the progress for Progress Bar but I am not able to get it.
class SendPhotoToFriend extends AsyncTask<String, String, String>{
String receiver,path = "";
public SendPhotoToFriend(String path,String receiver) {
// TODO Auto-generated constructor stub
this.path = path;
this.receiver = receiver;
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
ServiceDiscoveryManager sdm = ServiceDiscoveryManager
.getInstanceFor(connection);
if (sdm == null)
sdm = new ServiceDiscoveryManager(connection);
sdm.addFeature("http://jabber.org/protocol/disco#info");
sdm.addFeature("jabber:iq:privacy");
// Create the file transfer manager
FileTransferManager manager = new FileTransferManager(
connection);
FileTransferNegotiator.setServiceEnabled(connection, true);
// Create the outgoing file transfer
OutgoingFileTransfer transfer = manager
.createOutgoingFileTransfer(receiver + "/Smack");
System.out.println("Receiver of the file is "+receiver+"/smack");
Log.i("transfere file", "outgoingfiletransfer is created");
try {
long total = 0;
OutgoingFileTransfer.setResponseTimeout(30000);
transfer.sendFile(new File(path), "Description");
Log.i("transfere file", "sending file");
while (!transfer.isDone()) {
//Thread.sleep(1000);
total+=transfer.getProgress();
publishProgress(""+(int)((total*100)/transfer.getFileSize()));
Log.i("transfere file", "sending file status "
+ transfer.getStatus() + "progress: "
+ transfer.getProgress());
if (transfer.getStatus() == org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error) {
Log.i("transfere file", "Errorrr isss: "
+ transfer.getError()+transfer.getPeer());
transfer.cancel();
break;
}
}
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("transfere file", "sending file done");
return null;
}
@Override
protected void onProgressUpdate(final String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view =inflater.inflate(R.layout.user_chat_send_chat, null);
ProgressBar p_Bar = (ProgressBar)view.findViewById(R.id.progress_bar_image);
p_Bar.setProgress(Integer.parseInt(values[0]));
customAdapter1.notifyDataSetChanged();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
The progress Bar is the view of the custom list view, I want to update it at the run time.
Thanks
You're inflating the ProgressBar within the onProgress callback. This is absolutely wrong. The ProgressBar should to be added to the view before your AsyncTask starts (inflate and add) and then turn it to be visibile in your AsyncTask constructor. You should also store a reference to the ProgressBar in the AsyncTask (or somehow else in scope by non-static inner class).
In the end your AsyncTask should be looking like this (assuming the ProgressBar is already attached to the view):
class SendPhotoToFriend extends AsyncTask<String, String, String> {
String receiver,path = "";
ProgressBar progress;
public SendPhotoToFriend(String path, String receiver, ProgressBar progress) {
this.path = path;
this.receiver = receiver;
this.progress = progress;
this.progress.setVisibility(View.Visible);
}
@Override
protected String doInBackground(String... arg0) {
// You're application logic...
// ...
publishProgress((int)((total*100)/transfer.getFileSize()));
// ...
}
@Override
protected void onProgressUpdate(final Integer... values) {
this.progress.setProgress(values[0]);
}
}
Also you don't need to (and shouldn't) call adapter.notifiyDataSetChanged()
in your onProgres-Callback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With