我想在发送图像时实现进度条

时间:2013-03-23 10:10:58

标签: android android-progressbar

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

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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();

}
}