Reputation: 570
I want to send the image through socket and while the sending process going on the progress bar must be displayed and it should be updating when the image is being sending but when i tried this code the progressbar is not been displayed and the image is being send.
try {
//image send
client = new Socket(ServerIP,4444);
File file = new File(path);
byte[] mybytearray = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = client.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(file.getName());
int bytesread=0;
ProgressDialog pd = new ProgressDialog(c);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Sending...");
pd.setCancelable(false);
pd.setProgress(0);
pd.show();
while((bytesread=fis.read(mybytearray))>0)
os.write(mybytearray, 0, mybytearray.length);
int old_value=pd.getProgress();
int new_read=(int)( ((float)file.length()/bytesread));
int value= new_read+old_value;
pd.setProgress(value);
pd.dismiss();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
bis.close();
fis.close();
client.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 1
Views: 757
Reputation: 1493
public class UploadImage extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ShowFullImageShare.this);
pDialog.setMessage("Sending...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... args) {
while((bytesread=fis.read(mybytearray))>0)
os.write(mybytearray, 0, mybytearray.length);
int old_value=pd.getProgress();
int new_read=(int)( ((float)file.length()/bytesread));
int value= new_read+old_value;
os.write(mybytearray, 0, mybytearray.length);
os.flush();
bis.close();
fis.close();
client.close();
publishProgress(value);
return null;
}
@Override
protected void onProgressUpdate(Integer progress) {
pDialog.setProgress(progress);
}
@Override
protected void onPostExecute(Void value) {
pDialog.dismiss();
}
}
Upvotes: 0
Reputation: 22493
You have to look at AsyncTask for this type of task. try this following code and use for your requirement
public class UploadImage extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ShowFullImageShare.this);
pDialog.setMessage("Updating to twitter...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// write your code here for sending image
return null;
}
@Override
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
call this AsyncTask like this
new UploadImage().execute();
Upvotes: 1