gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:42:01 26 4
gpt4 key购买 nike

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

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

最佳答案

对于此类任务,您必须查看 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();

关于android - 我想在发送图像时实现进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15585539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com