1

我想通过套接字发送图像,并且必须显示进度条上的发送过程,并且在发送图像时它应该更新但是当我尝试此代码时,进度条没有显示并且图像正在发送.

  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();
    }
4

2 回答 2

1

对于此类任务,您必须查看 AsyncTask。尝试以下代码并用于您的要求

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();

    }
}

像这样调用这个 AsyncTask

new UploadImage().execute();
于 2013-03-23T10:24:37.147 回答
0
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();

}
}
于 2016-08-05T06:15:40.507 回答